I have a number of lists such as

I either need to make a list of lists or a dictionary containing lists. What would be the recommended one to do and will make the process of further sorting and text manipulation the easiest.

Thanks,

Recommended Answers

All 5 Replies

List is ordered and you mention sorting, so list of lists looks better. Difficult to tell without knowing more of your plans.

I have a loop which generates lists such as

Once all of this have been created within a list of dict I want to print them all out within correctly space|padded columns...

In the end Ive created a list of lists which looks like the following :

[['123', 19, '13', 'Fa0/19', '100000000'], ['13', 22, '13', 'Fa0/22', '100000000'], ['123', 19, '123', 'Fa0/19', '100000000']]

Can you confirm how the for loop would look if I wanted to go through each column by item ?

It is often usefull to do transpose and extract one of the columns which become own list, if you do not want simultanously to process all columns.

from pprint import pprint
info = [['123', 19, '13', 'Fa0/19', '100000000'], ['13', 22, '13', 'Fa0/22', '100000000'], ['123', 19, '123', 'Fa0/19', '100000000']]
print('Info')
pprint(info)
print('Transpose')
pprint(list(zip(*info)))
print('Fa by generator expression')
print(', '.join(fa for a, b, c, fa, d in info))

You can iterate through the list, taking each sub-list and printing the individual items within that sub-list.

test_list=[['123', 19, '13', 'Fa0/19', '100000000'],
           ['13', 22, '13', 'Fa0/22', '100000000'],
           ['123', 19, '123', 'Fa0/19', '100000000']]

for sub_list in test_list:
    print sub_list
    for ctr in range(len(sub_list)):
        print "     ", ctr, "-->", sub_list[ctr]
    print
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.