Home > OS >  Unable to unpack values in Python
Unable to unpack values in Python

Time:07-16

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?

CodePudding user response:

Please try using zip.

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

Output:

5903032523805 abc
5903031568925 xyz
5902976332061 yyy
  • Related