Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access and print the multiple values of a key in python?

In Python, a dictionary can only hold a single value for a given key. To work around this, our single value can be a list containing multiple values. Here we have a dictionary called "wardrobe" with clothing items and their colors. Fill in the blanks to print a line for each item of clothing with each color, for example: "red shirt", "blue shirt", and so on.

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

my code

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

I want to print it like red shirt, blue shirt, white shirt...

like image 348
Shivam Panchbhai Avatar asked Aug 30 '25 17:08

Shivam Panchbhai


1 Answers

In Python, a dictionary can only hold a single value for a given key. To workaround this, our single value can be a list containing multiple values. Here we have a dictionary called "wardrobe" with items of clothing and their colors. Fill in the blanks to print a line for each item of clothing with each color, for example: "red shirt", "blue shirt", and so on.

This gives the accurate required answer

wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}

for clothes,colors in wardrobe.items():

 for color in colors:
    
    print("{} {}".format(color,clothes))

output

 Here is your output:
 red shirt
 blue shirt
 white shirt
 blue jeans
 black jeans
like image 106
Rizwan Rao Avatar answered Sep 02 '25 08:09

Rizwan Rao