-1

I have a list of lists, called my_list:

['sit', (1, 1)]
['laboris', (2, 1)]
['nisi', (2, 1)]
['est', (4, 1)]
['qui', (4, 1)]
['cillum', (3, 1)]
['voluptate', (3, 1)]
['eu', (3, 1)]
['irure', (3, 1)]
['sunt', (4, 1)]
['reprehenderit', (3, 1)]
['nulla', (3, 1)]
['sint', (4, 1)]
['fugiat', (3, 1)]
['dolore', (2, 1)]
['dolore', (3, 1)]
['enim', (2, 1)]
['occaecat', (4, 1)]
['tempor', (2, 1)]
['commodo', (2, 1)]
['non', (4, 1)]
['minim', (2, 1)]
['aute', (3, 1)]
['ut', (2, 2)]
['ex', (2, 1)]
['deserunt', (4, 1)]
['ea', (2, 1)]
['eiusmod', (2, 1)]
['culpa', (4, 1)]
['labore', (2, 1)]
['mollit', (4, 1)]
['officia', (4, 1)]
['cupidatat', (4, 1)]
['adipiscing', (2, 1)]
['amet', (1, 1)]
['et', (2, 1)]
['ad', (2, 1)]
['consectetur', (2, 1)]
['anim', (4, 1)]
['magna', (2, 1)]
['quis', (2, 1)]
['ullamco', (2, 1)]
['dolor', (1, 1)]
['dolor', (3, 1)]
['aliquip', (2, 1)]
['velit', (3, 1)]
['ipsum', (1, 1)]
['incididunt', (2, 1)]
['sed', (2, 1)]
['id', (4, 1)]
['esse', (3, 1)]
['exercitation', (2, 1)]
['nostrud', (2, 1)]

I have tried:

d = {}

for item in all_lists:
    d[item[0]] = item[1:]

print (d)

But this overwrites a key, instead of updating that value. For example, dolor becomes: {'dolor': [(3,1)] instead of the desired goal of: {'dolor': (3,1), (1,1), etc...}

Ideally, the dictionary shape would not include a list of tuples as the value, but if need be.

How can I convert that list of lists to a dict in the format I would like?

I have observed Python: List of lists to dictionary but that yielded me the incorrect I have right now.

artemis
  • 6,857
  • 11
  • 46
  • 99
  • 4
    Possible duplicate of [list to dictionary conversion with multiple values per key?](https://stackoverflow.com/questions/5378231/list-to-dictionary-conversion-with-multiple-values-per-key) – rpanai Sep 17 '19 at 00:47

4 Answers4

3

Use defaultdict feature:

https://docs.python.org/2/library/collections.html#collections.defaultdict

Not tested code!

d = defaultdict(list)

for item in all_lists:
    d[item[0]].append(item[1:])

print (d)
bkbb
  • 237
  • 1
  • 6
1

You can use itertools.groupby in case the keys are already sorted (it looks like they are):

import itertools as it
result = {k: [x[1] for x in v] for k, v in it.groupby(test, key=lambda x: x[0])}
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • What if not sorted all the time? (In this case yes, but I think we are lucky) – artemis Sep 17 '19 at 01:15
  • @JerryM. You can always use `sorted` beforehand but for large lists with many different keys this might hurt performance and it's better to use `defaultdict` instead. – a_guest Sep 17 '19 at 08:27
0

One of the ways is to add new key-value pair if d is missing this key with value being empty list []. Then append new value to that list.

Python has setdefault to do that.

all_lists = [
    ['sit', (1, 1)],
    ['sit', (2, 2)],
    ['laboris', (2, 1)]
]

d = {}
for key, new_value in all_lists:
    values = d.setdefault(key, [])
    values.append(new_value)

print(d)


{
 'sit': [(1, 1), (2, 2)], 
 'laboris': [(2, 1)]
}
ovgolovin
  • 13,063
  • 6
  • 47
  • 78
-1

It's annoying but you can change the list to a tuple with a quick call to the constructor.

a = [
    ['sit', (1, 1)],
    ['laboris', (2, 1)],
    ['nisi', (2, 1)],
    ['est', (4, 1)],
    ['qui', (4, 1)],
    ['cillum', (3, 1)],
    ['voluptate', (3, 1)],
    ['eu', (3, 1)],
    ['irure', (3, 1)],
    ['sunt', (4, 1)],
    ['reprehenderit', (3, 1)],
    ['nulla', (3, 1)],
    ['sint', (4, 1)],
    ['fugiat', (3, 1)],
    ['dolore', (2, 1)],
    ['dolore', (3, 1)],
    ['enim', (2, 1)],
    ['occaecat', (4, 1)],
    ['tempor', (2, 1)],
    ['commodo', (2, 1)],
    ['non', (4, 1)],
    ['minim', (2, 1)],
    ['aute', (3, 1)],
    ['ut', (2, 2)],
    ['ex', (2, 1)],
    ['deserunt', (4, 1)],
    ['ea', (2, 1)],
    ['eiusmod', (2, 1)],
    ['culpa', (4, 1)],
    ['labore', (2, 1)],
    ['mollit', (4, 1)],
    ['officia', (4, 1)],
    ['cupidatat', (4, 1)],
    ['adipiscing', (2, 1)],
    ['amet', (1, 1)],
    ['et', (2, 1)],
    ['ad', (2, 1)],
    ['consectetur', (2, 1)],
    ['anim', (4, 1)],
    ['magna', (2, 1)],
    ['quis', (2, 1)],
    ['ullamco', (2, 1)],
    ['dolor', (1, 1)],
    ['dolor', (3, 1)],
    ['aliquip', (2, 1)],
    ['velit', (3, 1)],
    ['ipsum', (1, 1)],
    ['incididunt', (2, 1)],
    ['sed', (2, 1)],
    ['id', (4, 1)],
    ['esse', (3, 1)],
    ['exercitation', (2, 1)],
    ['nostrud', (2, 1)]
]

final = {}
for l in a:
    final[l[0]]=tuple(l[1:])[0]

print(final)

prints

{'sit': (1, 1), 'laboris': (2, 1), 'nisi': (2, 1), 'est': (4, 1), 'qui': (4, 1), 'cillum': (3, 1), 'voluptate': (3, 1), 'eu': (3, 1), 'irure': (3, 1), 'sunt': (4, 1), 'reprehenderit': (3, 1), 'nulla': (3, 1), 'sint': (4, 1), 'fugiat': (3, 1), 'dolore': (3, 1), 'enim': (2, 1), 'occaecat': (4, 1), 'tempor': (2, 1), 'commodo': (2, 1), 'non': (4, 1), 'minim': (2, 1), 'aute': (3, 1), 'ut': (2, 2), 'ex': (2, 1), 'deserunt': (4, 1), 'ea': (2, 1), 'eiusmod': (2, 1), 'culpa': (4, 1), 'labore': (2, 1), 'mollit': (4, 1), 'officia': (4, 1), 'cupidatat': (4, 1), 'adipiscing': (2, 1), 'amet': (1, 1), 'et': (2, 1), 'ad': (2, 1), 'consectetur': (2, 1), 'anim': (4, 1), 'magna': (2, 1), 'quis': (2, 1), 'ullamco': (2, 1), 'dolor': (3, 1), 'aliquip': (2, 1), 'velit': (3, 1), 'ipsum': (1, 1), 'incididunt': (2, 1), 'sed': (2, 1), 'id': (4, 1), 'esse': (3, 1), 'exercitation': (2, 1), 'nostrud': (2, 1)}
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • This fails with the example (`dolor`) I posted. – artemis Sep 17 '19 at 00:53
  • `dict` cannot contain duplicate keys as the strings would have the same hash. If you want to key the `dict` by something else then ok but attempting to write a second value with the same key will always fail. – JBirdVegas Sep 17 '19 at 00:55
  • Unless it is contained as a tuple of tuples or a list of tuples, as others have posted. – artemis Sep 17 '19 at 00:56