Home > Enterprise >  How to iterate through a list in a dictionary
How to iterate through a list in a dictionary

Time:07-05

If I want to print: red shirt blue shirt black jeans And etc. I have this code:

wardrobe = {"shirt": ["red", "blue", "white"], "jeans":["blue", "black"]}
for shirts in wardrobe.keys():
   for colors in shirts:
      print("{} {}".format(colors, shirts))

I get: s shirt h shirt i shirt and so on...

Suggestions anybody? Thank you!

CodePudding user response:

My suggestion is to use an IDE that allows you to debug and run line by line. That will help you understand better what your code is doing.

But the way you want to do this is use .items() to iterate through the key, values of the dictionary. The value is a list of your colors, so then iterate through that list.

wardrobe = {"shirt": ["red", "blue", "white"], "jeans":["blue", "black"]} 
            
for article, color_list in wardrobe.items(): 
    for color in color_list:
        print("{} {}".format(color, article))

CodePudding user response:

Use two nested loops, as you're doing, but make sure that in the inner loop you're iterating over the value, not the key. Iterating over items() will give you the key and the value as two different variables, making it easy:

>>> wardrobe = {"shirt": ["red", "blue", "white"], "jeans":["blue", "black"]}
>>> for article, colors in wardrobe.items():
...     for color in colors:
...         print(f"{color} {article}")
...
red shirt
blue shirt
white shirt
blue jeans
black jeans

CodePudding user response:

You need to iterate over both the keys and the values so in Python 3 you can use the .items() method as described here.

To correctly iterate over the colors for each clothing item you need to iterate through the items and then each colour in the list. Something like this should get you close:

wardrobe = {"shirt": ["red", "blue", "white"], "jeans":["blue", "black"]}
for item, colors in wardrobe.items():
   for color in colors:
      print(color, item)

red shirt
blue shirt
white shirt
blue jeans
black jeans

CodePudding user response:

warddrobe={"shirt":["white","red","green"],"jeans":["black","blue"]}
for shirt in warddrobe["shirt"]:
    print(f"{shirt} shirt")
for jeans in warddrobe["jeans"]:
    print(f"{jeans} jeans")

    ```
`
white shirt
red shirt
green shirt
black jeans
blue jeans
`
`
you just have to grab values thruogh indexing then simply print .
you need to use two for loop for it.
`
    

CodePudding user response:

The overall logic you tried is commendable.

> wardrobe = {"shirt": ["red", "blue", "white"], "jeans":["blue", "black"]}
> for shirts in wardrobe.keys():
>    for colors in shirts:
>        print("{} {}".format(colors, shirts))

But what you have done is you are iterating over the keys of the dictionary, not on it's items.

The answer is,

wardrobe = {"shirt": ["red", "blue", "white"], "jeans":["blue", "black"]}
for dict_keys,dict_values in wardrobe.items():
    for colors in dict_values:
       print("{} {}".format(colors, dict_keys))

CodePudding user response:

>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> for key in a_dict:
...     print(key)

this will print color , fruit and pet becoz it only iterate over the keys in the dictionary. Have a nice day

  • Related