Append Multiple Values for One Key in a Dictionary

append multiple values for one key in a dictionary

If I can rephrase your question, what you want is a dictionary with the years as keys and an array for each year containing a list of values associated with that year, right? Here's how I'd do it:

years_dict = dict()

for line in list:
if line[0] in years_dict:
# append the new number to the existing array at this slot
years_dict[line[0]].append(line[1])
else:
# create a new array in this slot
years_dict[line[0]] = [line[1]]

What you should end up with in years_dict is a dictionary that looks like the following:

{
"2010": [2],
"2009": [4,7],
"1989": [8]
}

In general, it's poor programming practice to create "parallel arrays", where items are implicitly associated with each other by having the same index rather than being proper children of a container that encompasses them both.

python 3 adding multiple values under same key in dictionary

I tried using: children[parent].append(child)

That will work as long as you are using lists for your dictionary-values.

The easiest fix to achieve that would be to make children a defaultdict, i.e.

from collections import defaultdict
children = defaultdict(list)

and then use

children[parent].append(child)

in your code.

Demo:

>>> from collections import defaultdict
>>> children = defaultdict(list)
>>>
>>> children['Peter'].append('Bob')
>>> children['Peter'].append('Alice')
>>> children['Mary'].append('Joe')
>>>
>>> children
defaultdict(list, {'Mary': ['Joe'], 'Peter': ['Bob', 'Alice']})

How to add multiple values in dictionary for a key

here if you want to add multiple numbers

a_list = []
a_dict = {'Number': a_list}

#here so the will loop will be runing
RUN = True

while RUN:
a = int(input('Enter a number: '))
l = a_list.append(a)
quit_the_while = input('Enter q to quit or any thing to add more number: ')
if 'q' in quit_the_while:
break
else:
continue

print(a_dict)

here for 3 numbers only

a_list = []
a_dict = {'Number': a_list}

while len(a_list) < 3:
a = int(input('Enter a number: '))
l = a_list.append(a)

print(a_dict)

How to add multiple values to a key in a Python dictionary

Dictionary, associative array or map (many names, basically the same functionality) property is that keys are unique.

The keys you wish to have, which are integers, are not unique if lengths are the same, that's why your code doesn't work. Putting a new value for existing key means replacing the old value.

You have to add key-value pairs to the existing value dictionaries.

for mykey in name_num:
length = len(name_num[mykey])
if length in new_dict: # key already present in new dictionary
new_dict[length][mykey] = name_num[mykey]
else:
new_dict[length] = {mykey: name_num[mykey]}

should do the trick

How to have multiple values for a key in a python dictionary?

You can't have multiple items in a dictionary with the same key. What you should do is make the value a list. Like this -

d = dict()
d["flow"] = ["flow"]
d["flow"].append("wolf")

If that is what you want to do, then you might want to use defaultdict. Then you can do

from collections import defaultdict
d = defaultdict(list)
d["flow"].append("flow")
d["flow"].append("wolf")


Related Topics



Leave a reply



Submit