6

Let us Suppose, I have created 3 lists and I want to create a dictionary for it. e.g.

a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]

Now What I want is to create a dictionary like this:

{'A':{'1' :'9'} , 'B':{'2':'8'}, 'C':{'3':'7'} , 'D':{'4':'6'}}

is it possible, Can Someone Help me on this?

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
TeSter
  • 119
  • 1
  • 11
  • 1
    Possible duplicate of [How to append multiple lists to a nested dictionary?](https://stackoverflow.com/questions/46831228/how-to-append-multiple-lists-to-a-nested-dictionary) – Georgy Jan 18 '19 at 10:29

7 Answers7

11

You can create the dictionary from zip-ed lists and convert the int values to strings - if I understood your question proper

dct = {x: {str(y): str(z)} for x, y, z in zip(a,b,c)}

Output:

{'A': {'1': '9'}, 'C': {'3': '7'}, 'B': {'2': '8'}, 'D': {'4': '6'}}
atru
  • 4,699
  • 2
  • 18
  • 19
4

You can also use map() here:

a = ['A', 'B', 'C', 'D']
b = [1, 2, 3, 4]
c = [9, 8, 7, 6]

dct = dict(map(lambda x, y, z : (x, {str(y): str(z)}), a, b, c))

print(dct)

Which outputs:

{'A': {'1': '9'}, 'B': {'2': '8'}, 'C': {'3': '7'}, 'D': {'4': '6'}}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
2

Assuming what you want is to have a be keys in the outer dictionary, and b and c the key and value element of the inner dicts:

d = {k: {x: y} for k, x, y in zip(a, b, c)}

Update: However, in your example x and y are strings, so if that's what you want:

d = {k: {str(x): str(y)} for k, x, y in zip(a, b, c)}
Thomas Fauskanger
  • 2,536
  • 1
  • 27
  • 42
2
a = ['A', 'B', 'C', 'D'] # don't forget the quotation marks
b = [1, 2, 3, 4]
c = [9, 8, 7, 6]

res = dict()
for i, index_a in enumerate(a):
    res[index_a] = {str(b[i]): c[i]}

Edit: Alternatively with list comprehension (mainly for the voters in here, as it's advanced python and harder to understand):

res = dict((a[i], {str(b[i]): c[i]}) for i in range(len(a)))
hansaplast
  • 11,007
  • 2
  • 61
  • 75
  • 1
    That's a generator expression, not a list comprehension. But you could turn it into a dict comprehension to make it cleaner: `{a[i]: {str(b[i]): c[i]} for i in range(len(a))}`. – wjandrea Sep 02 '20 at 16:51
2
{ a[x]: {b[x]: c[x]} for x in range(len(a))}

or if you really mean it:

{ a[x]: {str(b[x]): str(c[x])} for x in range(len(a))}
JJoao
  • 4,891
  • 1
  • 18
  • 20
2

You can try this:

a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
new_data = dict([[a, dict([map(str, i)])] for a, i in zip(a, zip(b, c))])

Output:

{'A': {'1': '9'}, 'C': {'3': '7'}, 'B': {'2': '8'}, 'D': {'4': '6'}}

Or

new_data = dict(zip(a, map(lambda x:dict([x]), zip(map(str, b), map(str, c)))))
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • In the first snippet, you don't need the outer list. `dict` can take a generator expression: `dict([a, dict([map(str, i)])] for a, i in zip(a, zip(b, c)))` – wjandrea Sep 02 '20 at 16:48
1

Are you looking for something like this ?

a= ['A', 'B', 'C', 'D']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]

new_dict={}
set(map(lambda x,y,z:(new_dict.__setitem__(x,{y,z})),a,b,c))
print(new_dict)

output:

{'D': {4, 6}, 'A': {9, 1}, 'B': {8, 2}, 'C': {3, 7}}
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • There's four problems with this: 1) [Don't use a comprehension for side effects](https://stackoverflow.com/q/5753597/4518341); use a for-loop instead. 2) [Don't use dunder methods manually](https://stackoverflow.com/a/2481433/4518341); use the equivalent expression instead. 3) `set(map(lambda))` is ugly. 4) OP wants dicts inside, not sets. So: `new_dict = {}; for x, y, z in zip(a, b, c): new_dict[x] = {y: z}`, which you can boil down into a comprehension, like in [atru's answer](https://stackoverflow.com/a/47944823/4518341): `new_dict = {x: {y: z} for x, y, z in zip(a, b, c)}` – wjandrea Sep 02 '20 at 16:58