0

I have two dictionaries with contents:

dct1 = {'NL': 7,'MC': 9, 'PG': 8}
dct2 = {'NL': 2,'MC': 10,'PG': 6}

You could say these represents scores from a game where the letters are names and the numbers are scores. The difference between the two dictionaries are the numbers in which they are calculated based on criteria.

Now i want to combine the contents from the dictionary into a list of list. I'm going to provide just a rough idea of my code. Basically what i did then was turning the contents in the two dictionaries into a list of list where:

L1 = [['NL',7],['MC',9],['PG',8]]
L2 = [['NL',2],['MC',10],['PG',6]]

The code for turning them into a list of list:

L1 = []
for i, occurrences in dct1.items():
    L1.append([i,occurrences])
L2 = []
for j, occurrences in dct2.items():
    L2.append([j,occurrences])

and once i print both list, i get as what I've written above.

But now, instead of having two different list, i want to combine both of them into a single list where my output is:

L3 = [['NL',7,2],['MC',9,10],['PG',8,6]]

Basically the single list does not have to repeat the letters twice and just adding the second digit. Any help is much appreciated.

Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
Electric
  • 517
  • 11
  • 25
  • Possible duplicate of [Converting Python Dictionary to List](http://stackoverflow.com/questions/1679384/converting-python-dictionary-to-list) – Jacopo Lanzoni Apr 20 '17 at 10:13

4 Answers4

9

A list comprehension should do:

lst =  [[k, v, dct2[k]] for k, v in dct1.items()]
print lst
# [['NL', 7, 2], ['PG', 8, 6], ['MC', 9, 10]]

Note that ordering of the sublists may vary, since dictionaries are not ordered.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • 1
    Wow.. Nailed it. – Rahul K P Apr 20 '17 at 10:24
  • @Moses Koledoye thanks a bunch. It helped. I'm not familiar with list comprehension but what if we were to have 3 or more dictionaries instead of 2? is this method only limited to 2 dictionaries? – Electric Apr 20 '17 at 10:37
  • @Electric No it's not. Simply add the dictionary value from the 3rd to nth to the inner sublist: `[[k, v, dct2[k], dct3[k], ..., dctn[k] for k, v in dct1.items()]` – Moses Koledoye Apr 20 '17 at 10:38
  • Easier to just make it generic at that points: `[[k, v] + [d[k] for d in dicts[1:]] for k, v in dicts[0].items()]` – Anonymous Apr 20 '17 at 10:46
  • @Moses Koledoye adding a third dictionary after dct2[k] produces an error saying 'KeyError: 'NL'. Any suggestions? – Electric Apr 20 '17 at 10:52
  • 1
    @Electric Well, that's because your dictionaries are not homogenous. Replace all `dct[k]` with `dct.get(k)` – Moses Koledoye Apr 20 '17 at 10:53
1

As key is same in both dictionary:

>>> dct1 = {'NL': 7,'MC': 9, 'PG': 8}

>>> dct2 = {'NL': 2,'MC': 10,'PG': 6}
>>> L3 = []
>>> for key in dct1:
...     L3.append([key, dct1[key], dct2[key]])
... 
>>> L3
[['NL', 7, 2], ['PG', 8, 6], ['MC', 9, 10]
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
0

You can use list comprehension to put the items in a list. Also, use get() method on dict so that it does not throw key error if the key is not present in the other dict.

>>> [(key, val, dct1.get(key)) for key, val in dct2.items()]
[('NL', 2, 7), ('PG', 6, None), ('MC', 10, 9)]
Charul
  • 450
  • 5
  • 18
0

Assuming you are using python 2.7.x

For understanding

L3 = []
for key, value in dct1.iteritems():
    L3.append([key, value, dct2[key])

OR

Short and Sweet using List comprehension :

L3 =  [[key, value, dct2[key]] for key, value in dct1.iteritems()]
Vishvajit Pathak
  • 3,351
  • 1
  • 21
  • 16