Click here to Skip to main content
15,436,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i separate the values of my dictionary in the for loop like this, with .items() does anyone know how to do that? or is there a better way?

this wil now give me:

[2, 0]
[3, 0]
4

i would like to print the 2 and the 0 etc. separate from each other.

What I have tried:

Python
producten = {'appel': [2, 0], "banaan": [3, 0], 'peer': 4}


for product, prijs in producten.items():
    print(prijs[1])
Posted
Updated 22-Sep-20 22:34pm

You can do Unzip (reverse of zip) tuples using a * and zip.

Example:
Python
producten = {'appel': [2, 0], "banaan": [3, 0], 'peer': 4}
  
keys, values = zip(*producten.items()) 
  
print ("keys : ", str(keys)) 
print ("values : ", str(values)) 

#output
('keys : ', "('appel', 'banaan', 'peer')")
('values : ', '([2, 0], [3, 0], 4)')

Refer: Using * and zip to 'unzip'[^]
 
Share this answer
 
Try this:
Python
producten = {'appel': [2, 0], "banaan": [3, 0], 'peer': 4}
for product, prijs in producten.items():
    if type(prijs) is list:
        for item in prijs:
            print(item)
    else:
        print(prijs)

It is probably not the exact answer you are looking for but is shows you how to separate the values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900