0

I have a dictionary say

a = { 'a': 'abc',
'b': 'xyz',
'c': 'yyy'

}

and another list of dictionaries say

list_dict = [{'id': 5903032523805, 'value': 'aaa'}, {'id': 5903031568925, 'value': 'bbb'}, {'id': 5902976332061, 'value': 'ccc'}]

How do I perform

for i, j in list_dict, a.values():
    print(i['id'], j)

Error: too many values to unpack (expected 2)

Now i'm aware that a.values returns dict_values[()] as individual items (iterate) as output and also i['id'] returns first dictionary id (1st item in list). But when combined in a for loop , unable to extract it. Any suggestions as to what I'm doing wrong?

2
  • Does this answer your question? python: ValueError: too many values to unpack (expected 2) Commented Jul 15, 2022 at 18:11
  • For an explanation of what's going on: What you've done here is create a tuple with the following structure: (dict, List[dict]), so it will unload the first element (the dict) but it expects 3 values instead of two since that's how many keys are in your dict. Commented Jul 15, 2022 at 18:16

1 Answer 1

2

Please try using zip.

for (i, j) in zip(list_dict, a.values()):
     print(i['id'], j)

Output:

5903032523805 abc
5903031568925 xyz
5902976332061 yyy
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Liju. I had studied about zip(zipping two datastructure values into one single tuple like ((,), (,)) but did not remember to use it here.

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.