-1

I want to create a dictionary with multiple values in the form of a list for value of a key(To be precise for key '0').But when I use the zip function it is only giving the last updated value for that key instead of all values attached to that key. Below is the code.

values_list = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
keys_list = [0,0,0,1,0,0,2,-1,0]
my_dict = dict(zip(keys_list, values_list))
print(my_dict)

Output from above code:

{0: [2, 2], 1: [1, 0], 2: [2, 0], -1: [2, 1]}

Required Output:

{0: [[0, 0],[0, 1],[0, 2],[1, 1],[1, 2],[2, 2]], 1: [1, 0], 2: [2, 0], -1: [2, 1]}

mad_mouse
  • 1
  • 1
  • 6
    Your "Required Output" is not valid Python – Iain Shelvington Jun 05 '21 at 05:06
  • "I want to create a dictionary with multiple values for value of a key" There is no such thing. But *as you have already observed*, a dictionary value can be a list, and a list can have multiple elements. Unfortunately, there is no "slick" way to build this structure - there are some helpers like `defaultdict` but fundamentally you will still have to iterate and append elements to the value lists. – Karl Knechtel Jun 05 '21 at 05:09
  • 1
    Does this answer your question? [append multiple values for one key in a dictionary](https://stackoverflow.com/questions/3199171/append-multiple-values-for-one-key-in-a-dictionary) – ThePyGuy Jun 05 '21 at 05:10
  • You should look into tree/graph. – Yoshikage Kira Jun 05 '21 at 05:11
  • @Don'tAccept No, I cannot relate to my question. – mad_mouse Jun 05 '21 at 05:57
  • @Iain Shelvington May be there is something wrong with my explaination. But basically I want a list of mutliple values for a value of a particular key.(To be precise for key '0') – mad_mouse Jun 05 '21 at 05:59

3 Answers3

1

As pointed out, the format you asked isn't valid python (as it has entries without a key), but I believe the following works for you:

values_list = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
keys_list = [0,0,0,1,0,0,2,-1,0]
my_dict = {}
for value, key in zip(values_list, keys_list):
    if key in my_dict:
        my_dict[key].append(value)

    else:
        my_dict[key] = [value]

print(my_dict)

which would output: {0: [[0, 0, [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]], 1: [[1, 0]], 2: [[2, 0]], -1: [[2, 1]]}

dzshn
  • 56
  • 1
  • 5
0

Let's say you want to get '[0, 0],[0, 1],[0, 2]' form as value, It is impossible.

You sould cover [0, 0], [0, 1], [0, 2] with list or tuple.

list -> [ [0, 0], [0, 1], [0, 2] ] tuple -> ( [0, 0], [0, 1], [0, 2] )

But still, you want to stay that form you can make it as str. -> I don't recommend it. Because it will be hard to parse

이성진
  • 51
  • 5
  • May be there is something wrong with my explaination. But basically I want a list of mutliple values for a value of a particular key.(To be precise for key '0') – mad_mouse Jun 05 '21 at 06:00
0

If you don't want to use defaultdict, you can yourself achieve similar to what defaultdict provides: Don't create the dictionary directly from zip, instead iterate it and call dict.get() method with default value as empty list [], and follow the logic.

my_dict = dict()
for key,value in zip(keys_list, values_list):
    my_dict.update({key: my_dict.get(key, [])+[value]})

OUTPUT:

{0: [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]], 1: [[1, 0]], 2: [[2, 0]], -1: [[2, 1]]}

All the values in Output will have nested list, if you want to have nested list only for the keys which has more than one value, you can first check if the key exists.

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45