2

I have list of lists:

[['13/03/2012', ['a']], ['13/03/2012', ['b', 'c', 'd']], ['13/03/2012', ['e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]

I need the list contain the same date in each first value will join the second value to output like this

[['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]

Please anyone can help me?

kuslahne
  • 720
  • 4
  • 10
  • 21

3 Answers3

6

You could try using itertools. This groups the list by date and then iterates through the keys/groups, creating a list that has the key as the first element and the 'flattened' list values:

In [51]: from itertools import groupby

In [52]: result = []

In [53]: for key, group in groupby(l, key=lambda x: x[0]):
   ....:     inner = [key, [item for subg in group for item in subg[1]]]
   ....:     result.append(inner)
   ....:
   ....:

In [54]: result
Out[54]:
[['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']],
 ['26/03/2012', ['f']],
 ['02/04/2012', ['a']],
 ['09/04/2012', ['b']]]

You could do this as a one-liner, but besides being over 80 characters, it is even less readable than the first version and should probably be avoided :)

In [57]: result = [[key, [item for subg in group for item in subg[1]]] for key, group in groupby(l, key=lambda x: x[0])]

In [58]: result
Out[59]:
[['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']],
 ['26/03/2012', ['f']],
 ['02/04/2012', ['a']],
 ['09/04/2012', ['b']]]
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
2

I would suggest you go through these links first

for_loops

ways-to-create-dictionary

how-can-i-convert-a-python-dictionary-to-a-list-of-tuples

this should get you through..

In [7]: [['13/03/2012', ['a']], ['13/03/2012', ['b', 'c', 'd']], ['13/03/2012', ['e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]

In [8]: d= {}

In [9]: for item in l:
   ...:     if d.has_key(item[0]):
   ...:         d[item[0]].extend(item[1])
   ...:     else:
   ...:         d[item[0]] = item[1]
   ...:

In [10]: d
Out[10]:
{'02/04/2012': ['a'],
 '09/04/2012': ['b'],
 '13/03/2012': ['a', 'b', 'c', 'd', 'e', 'f'],
 '26/03/2012': ['f']}

In [11]: [[k,v] for k,v in d.items()]
Out[11]:
[['02/04/2012', ['a']],
 ['09/04/2012', ['b']],
 ['26/03/2012', ['f']],
 ['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']]]
Community
  • 1
  • 1
avasal
  • 14,350
  • 4
  • 31
  • 47
  • Thanks @avasal, I add sorted(d.items()) before convert to list. Also changed the date format to "%Y/%m/%d" make more good on sortable. – kuslahne Nov 29 '12 at 07:11
1

Similar to avasal, but this seems like a good place to use a defaultdict

from collections import defaultdict
l = [['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]
d = defaultdict(list)
for item in l:
    d[item[0]].extend(item[1])

print map(list, d.items())
wye.bee
  • 708
  • 4
  • 11
  • This is what i would do when BUILDING the list (if possible). Then from the dictionary, if a list is still needed, I would convert from the dictionary to a list object. – Ross Nov 29 '12 at 04:26