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,

Dani AI

Generated

Short practical note tied to the thread: wants easy sorting and nicely padded column output. Both approaches work, but pick by access pattern. If columns are positional and records are uniform, a list-of-lists is simple and fast (as pointed out). If columns need to be referenced by name, or some rows may omit fields, convert to a list of dicts (or namedtuples/dataclasses) — that often makes sorting and formatting more maintainable than juggling numeric indexes. 's nested-iteration idea is fine for inspection; for aligned output it's useful to compute column widths first and format rows with a single format string.

Example pattern (convert rows to dicts, compute widths, print header and rows aligned):

# fields is the column names in order; rows is the list-of-lists
fields = ['id', 'count', 'tag', 'iface', 'speed']
dict_rows = [dict(zip(fields, row)) for row in rows]

# compute widths (treat values as strings)
widths = {f: max(len(f), max(len(str(r.get(f,''))) for r in dict_rows)) for f in fields}
fmt = '  '.join('{' + f + ':' + str(widths[f]) + '}' for f in fields)

# header and rows
print(fmt.format(**{f: f for f in fields}))
for r in dict_rows:
    print(fmt.format(**{f: str(r.get(f,'')) for f in fields}))

# sort in-place by numeric column 'count'
dict_rows.sort(key=lambda r: int(r.get('count', 0)))

Notes and cautions: coerce types before measuring widths (ints -> str), handle missing values with .get(..., '') and be explicit about numeric sorting (convert to int/float in key). For clarity and type safety in larger projects, prefer collections.namedtuple or dataclasses for records instead of anonymous dicts.

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.