Skip to content Skip to sidebar Skip to footer

Combining Lists Within A Nested List, If Lists Contain The Same Element?

I have nested list that has a structure similar to this, except it's obviously much longer: mylist = [ ['Bob', '12-01 2:30'], ['Sal', '12-01 5:23'], ['Jill', '12-02 1:28'] ] My go

Solution 1:

Use dict or orderdict(if the sort is important) group data by the date time .

from collections import defaultdict # use defaultdict like {}.setdefault(), it's very facility

mylist = [["Bob", "12-01 2:30"], ["Sal", "12-01 5:23"], ["Jill", "12-02 1:28"]]
record_dict = defaultdict(list)
# then iter the list group all date time.for data in mylist:
    _, time = data
    date_time, _ = time.split(" ")
    record_dict[date_time].append(data)

res_list = list(record_dict.values())
print(res_list)

output: [[['Bob', '12-01 2:30'], ['Sal', '12-01 5:23']], [['Jill', '12-02 1:28']]]

Solution 2:

A pure list-based solution as an alternative to the accepted dictionary-based solution. This offers the additional feature of easily sorting the whole list, first by date, then by hour, then by name

from itertools import groupby

mylist = [["Bob", "12-01 2:30"], ["Sal", "12-01 5:23"], ["Jill", "12-02 1:28"]]

newlist = [dt.split() + [name] for (name, dt) in mylist]
newlist.sort() # can be removed if inital data is already sorted bydate
newlist = [list(group) for (date, group) in groupby(newlist, lambda item:item[0])]

# result:
# [[['12-01','2:30','Bob'], ['12-01','5:23','Sal']], [['12-02','1:28','Jill']]]

If you really want the same item format as the initial list, it requires a double iteration:

newlist = [[[name, date+' '+time] for (date, time, name) ingroup]
           for (date, group) in groupby(newlist, lambda item:item[0])]

# result:
# [[['Bob', '12-01 2:30'], ['Sal', '12-01 5:23']], [['Jill', '12-02 1:28']]]

Solution 3:

If you don't mind going heavy on your memory usage, you can try using a dictionary. You can use the date as the key and make a list of values.

all_items = {}
for line in myList:
    x, y = line
    date, time = y.split()
    try:
        all_items[date].append(line)
    except:
        all_items[date] = [line,]

Then, you can create a new list using the sorted date for keys.

Solution 4:

If all of the elements with the same date are consecutive, you can use itertools.groupby:

list(map(list, groupby(data, lambda value: ...)))

Post a Comment for "Combining Lists Within A Nested List, If Lists Contain The Same Element?"