1

Alright I searched, but I did not find an answer to my specific problem. I am creating a dictionary of lists. I have while loop creating a few lists of values as well as a name to associate with it:

dict = {} 
list = [1, 2]
name = key1
list = [3, 4]
name = key1

If the name is the same, the list will be appended to the existing set of values for the key. Here is the code I have:

if key in dict:
    dict[key].append(list)
else:
    dict[key] = list

This is the output I want:

dictionary = {'key1': [1, 2], [3, 4]}

However I keep getting this output:

dictionary = {'key1': [1, 2, [3, 4]]}

where the second list to the key is being put inside of the first list.

Saksham Varma
  • 2,122
  • 13
  • 15
MarcAlex
  • 33
  • 3
  • 3
    In the output you want you seem to have a key with 2 values. That's not going to happen. should it perhaps be dictionary = `{'key1': [[1, 2], [3, 4]]}` or h 2 values. That's not going to happen. should it perhaps be dictionary = `{'key1': [1, 2, 3, 4]}` – Leon Apr 19 '15 at 19:51
  • 2
    don't name your variables as `list` and `dict` as they shadow the builtin – Bhargav Rao Apr 19 '15 at 19:51

4 Answers4

4

This is a very common error. I am fairly certain that you are doing:

list1.append(list2)

Instead, you want to do:

list1.extend(list2)

Here is a very useful resource

However, since you want [[1,2], [3,4]] instead of [1,2,3,4], you should do:

if key in d1:
    d1[key].append(l1)
else:
    d1[key] = [l1]
Community
  • 1
  • 1
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • That's a little better, but extend gives me: dict = {[1, 2, 3, 5]} instead of dict = {[1, 2], [3, 4]} – MarcAlex Apr 19 '15 at 19:58
  • 1
    @MarcAlex, What do you mean by `[1,2], [3,4]`? That is not possible. A dictionary cannot have multiple values for the same key. Did you mean `[[1,2], [3,4]]` – sshashank124 Apr 19 '15 at 20:02
  • Yeah, I guess I did mean to create a list of lists in the dictionary. So I would nee [[1, 2], [3, 4]] in the dictionary. – MarcAlex Apr 19 '15 at 20:04
2

That is because you are appending a list to a list each time. You need to use extend instead. Code:

keys = ['name1', 'name2', 'name2', 'name1']
somelist = [1, 2, 3]
d = {}
for k in keys:
   d.setdefault(k, []).extend(somelist)
Saksham Varma
  • 2,122
  • 13
  • 15
2

You need a list of list in fact, your output will look like this:

dictionary = {'key1': [[1, 2], [3, 4]]}

To have a key associated to multiple values you could use this line:

dictionary.setdefault(key, []).append(a_list)

setdefault will associate the key to the default value [] if the key is not present in your dictionary.

Also you should avoid using dict or list to define your own variables, they are built-in and you are redefining them.

Edit

To make it obvious to the reader maybe this could help, its the output of an interative python session:

>>> d = {}
>>> d.setdefault('key1', []).append([1, 2])
>>> d.setdefault('key1', []).append([3, 4])
>>> d
{'key1': [[1, 2], [3, 4]]}
Lynch
  • 9,174
  • 2
  • 23
  • 34
1

I hope not misunderstand , since dictionary = {'key1': [1, 2], [3, 4]} is poorly expressed

def add2dict(d, l, k):
  if not k in d:
    dict[k] = []
  d[k].append(l)

dict = {} 
add2dict(dict, [1,2], "key1")
add2dict(dict, [3,4], "key1")

result in dict:

{'key1': [[1, 2], [3, 4]]}

Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62