0

I am trying to add items to a list in a dictionary. I have two lists: x_list and y_list. I am trying to make x_list the keys and y_list the values. I have tried using a zip method but I really need to add them one by one. Right now I have:

dictionary = dict((x,0) for x in x_list)

but I would like to have something like:

dictionary = dict((x,y) for x in x_list, for y in y_list)

but obviously this is creating a syntax error. Is there any way to do this? Thank you!

EDIT:
I have tried zipping and it works, thank you, but I need to add the items to the dictionary one by one (I'm trying to have entries with the same keys add the values together for instance apple:10 and apple:5 become apple:15)

FOR EXAMPLE:

x_list = (blue, orange, purple, green, yellow, green, blue)
y_list = (1, 2, 5, 2, 4, 3, 8)

I would like the output to be

dictionary = {blue:9, orange:2, purple:5, green:5, yellow:4}

and the lists are continuously added to.

7
  • 2
    You can use dict(zip(x_list,y_list)). Commented Apr 17, 2017 at 18:00
  • I have tried zipping and it works, thank you, but I need to add the items to the dictionary one by one (I'm trying to have entries with the same keys add the values together for instance apple:10 and apple:5 become apple:15) Commented Apr 17, 2017 at 18:03
  • 1
    @okisker Update the question with this detail Commented Apr 17, 2017 at 18:04
  • 1
    @okisker: I think you better provide some sampe input/output such that it is clear what you aim to do. Commented Apr 17, 2017 at 18:04
  • 1
    That sounds like a scenario where you would want a Counter Commented Apr 17, 2017 at 18:05

4 Answers 4

1

I would use a Counter here:

from collections import Counter

c = Counter()
for k, v in zip(x_list, y_list):
    c[k] += v
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

dct = {}
x_list = (blue, orange, purple, green, yellow, green, blue)
y_list = (1, 2, 5, 2, 4, 3, 8)
for i in range(len(x_list)):
    if x_list[i] in dct.keys():
        dct[x_list[i]] += y_list[i]
    else:
        dct[x_list[i]] = y_list[i]

print dct

Comments

0

Short solution using enumerate function:

x_list = ['blue', 'orange', 'purple', 'green', 'yellow', 'green', 'blue']
y_list = [1, 2, 5, 2, 4, 3, 8]
result = {}

for i, v in enumerate(x_list):
    result[v] =  y_list[i] if not result.get(v) else result[v] + y_list[i]

print(result)

The output:

{'yellow': 4, 'orange': 2, 'blue': 9, 'green': 5, 'purple': 5}

Comments

0

Try this code:

list_x =["apple", "mango", "orange", "apple","mango"]
list_y = [10,5,25,10,15]
total_dict = {}
for k, v in zip(list_x, list_y):
    total_dict[k] = total_dict.get(k,0)+v 

The value of total_dict ends up as:

{'orange': 25, 'mango': 20, 'apple': 20}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.