-1

If I have a list of numbers and want to add them to an existing dictionary with the value 0. What's the easiest way of doing this? Seems so easy but I can't find any clean way of doing this. For example:

key = [1,2,3]
value = 0
dict  = {}
... one more line of code if possible ...

>>>dict
{(1,2,3):0}
eneas max
  • 81
  • 1
  • 3

2 Answers2

1

You could change your list to a tuple like this:

key = [1,2,3]
value = 0
d = {tuple(key): value}

>>>d
{(1,2,3):0}
GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40
0

Your desired output uses a tuple as the key. So convert the list to a tuple.

d[tuple(key)] = 0

BTW, don't use dict as a variable name, it's the name of a built-in type/function.

Barmar
  • 741,623
  • 53
  • 500
  • 612