Skip to content
Advertisement

How can I retrieve a list of values in an array that has same name but different version? [closed]

For example if I have a list:

[
    ["nameoffile1","version1","date02/03"],
    ["nameoffile1","version2","date02/04"],
    ["nameoffile2","version2","date05/02"],
    ["nameoffile3","version3","date02/04"]
]

if I have to retrieve just the nameoffile1 based on names how can I retrieve those 2 arrays based on names

thanks

Advertisement

Answer

You can use a dictionary to collect together all the items with the same name. If you use defaultdict to create an empty list the first time a name is encountered, it becomes trivial.

from collections import defaultdict
d = defaultdict(list)
for row in lst:
    d[row[0]].append(row)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement