+ 1

Merging dictionaries to a list

I need this: x = {a:1, b:2} y = {c:3, d:4} to become this: z = [{a:1, b:2}, {c:3, d:4}] How do I go about it?

26th Apr 2020, 1:54 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
4 Respostas
+ 6
You can do this: z = [ x , y ]
26th Apr 2020, 2:09 PM
Tricker
+ 7
The issue is that you need to put quotes around the characters. Otherwise the code will not run. The rest is ok: x = {'a':1, 'b':2} y = {'c':3, 'd':4} z = [x, y] print(z) #output: #[{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
26th Apr 2020, 2:12 PM
Lothar
Lothar - avatar
+ 2
What is the problem with z = ........ You have done it correctly
26th Apr 2020, 1:59 PM
XXX
XXX - avatar
0
Done. Thanks.
26th Apr 2020, 2:12 PM
Tomiwa Joseph
Tomiwa Joseph - avatar