5

How to create a dictionary of dictionaries from existing lists of keys and values?

celebr = ['Tony','Harry','Katty','Sam']
perc = [69,31,0,0]

d = dict(zip(celebr, perc))
dlist = []
for i in d.items():
    dlist.append(i)    
print(dlist)

Output:

[('Tony': 69), ('Harry': 31), ('Katty': 0), ('Sam': 0)]

When I use d.items it automatically gives me tuples, not dictionaries. Is there a way of creating a list of dictionaries, not tuples?

I need to get the following structure:

[{'Tony': 69}, {'Harry': 31}, {'Katty': 0}, {'Sam': 0}]
Georgy
  • 12,464
  • 7
  • 65
  • 73
Ivan Shelonik
  • 1,958
  • 5
  • 25
  • 49
  • the structure you desire is a `list` of `set`s; not of `dictionaries`. Do you mean `[{'Tony': 69}, {'Harry': 31}, {'Katty': 0}, {'Sam': 0}]`? And if yes, why? Would it not be better to have all entries in a single `dictionary` like `{'Tony': 69, 'Harry': 31, 'Katty': 0, 'Sam': 0}`? – Ma0 Aug 07 '17 at 08:06
  • Look here: https://stackoverflow.com/questions/3783530/python-tuple-to-dict – zypro Aug 07 '17 at 08:18
  • Sorry, yes, I want a list of dictionaries, that was a mistake – Ivan Shelonik Aug 07 '17 at 08:19
  • 2
    @IvanShelonik what advantage does a list of dicts give you over a list of tuples? unless you have non-unique names why not just use a single dict? – ragardner Aug 07 '17 at 08:20

6 Answers6

11

Good use case for list comprehension:

dlist = [{k: v} for k, v in zip(celebr, perc)]

Output:

>>> celebr = ['Tony', 'Harry', 'Katty', 'Sam']
>>> perc = [69, 31, 0, 0]
>>>
>>> [{k: v} for k, v in zip(celebr, perc)]
[{'Tony': 69}, {'Harry': 31}, {'Katty': 0}, {'Sam': 0}]
ettanany
  • 19,038
  • 9
  • 47
  • 63
3

Would this be enough

celebr = ['Tony','Harry','Katty','Sam']
perc = [69,31,0,0]

dlist = []

for i, j in zip(celebr, perc):
    dlist.append({i: j})

print dlist
Dror Av.
  • 1,184
  • 5
  • 14
Naren Murali
  • 19,250
  • 3
  • 27
  • 54
3

You can implement with zip and dict.

dict(zip(celebr,perc))

Results:

In [14]: celebr = ['Tony', 'Harry', 'Katty', 'Sam'] 
In [15]: perc = [69, 31, 0, 0]

In [16]: dict(zip(celebr,perc))
Out[16]: {'Harry': 31, 'Katty': 0, 'Sam': 0, 'Tony': 69}
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
1

Your example merely creates a list containing the dicts items. Your question asks for a list of sets. If you really want individual dicts then use a colon to separate the key and the value:

>>> [{k, v} for (k, v) in d.items()]
[{'Tony', 69}, {'Harry', 31}, {'Katty', 0}, {'Sam', 0}]
>>> [{k: v} for (k, v) in d.items()]
[{'Tony': 69}, {'Harry': 31}, {'Katty': 0}, {'Sam': 0}]
holdenweb
  • 33,305
  • 7
  • 57
  • 77
1

Try :

celebr = ['Tony','Harry','Katty','Sam']
perc = [69,31,0,0]

b = [{celebr[i]: perc [i]} for i in range(0, len(celebr))]

print(b)

Output :

[{'Tony': 69}, {'Harry': 31}, {'Katty': 0}, {'Sam': 0}]
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
0

list comprehension seems to be the shortest and easiest way and also the python way to do this, but I think if someone find list comprehension difficult, they could first make a dictionary and then append to an empty list as follows:

keys = [1,2,3,4,5,6,7]
values = ['a','b','c','d','e','f','g']
counter = 0
lst = []
for key in keys:
    dct = {keys[counter]:values[counter]}
    lst.append(dct)
    counter += 1
print(lst)

output:

[{1: 'a'}, {2: 'b'}, {3: 'c'}, {4: 'd'}, {5: 'e'}, {6: 'f'}, {7: 'g'}]