Shivam Panchbhai
Shivam Panchbhai

Reputation: 85

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...

Upvotes: 4

Views: 9743

Answers (5)

Raymond Teoh
Raymond Teoh

Reputation: 1

  1. In the first for loop, we iterate the items in dictionary. Variable apparel is use to iterate the key such as "shirt and jeans" and variable color use to iterate the values of the key such as "red,blue,white etc.."

  2. In the second for loop, variable i is use to iterate the list in color value.

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

Below give you an idea on how the value of the variables changed when the code execute.

2nd line: Apparel = "Shirt" , Color = ["red","blue","white"]
3rd line: i = "red"
4th line: i = "red", apparel = "Shirt"
Repeat 3rd line to finish iterate the values
3rd line: i = "blue"
4th line: i = "blue", apparel = "Shirt"
and so on...

Upvotes: 0

Nelson Luluca
Nelson Luluca

Reputation: 11

  1. use the method .items(), to access the dictionary.
  2. then give a variable name for the keys and values inside of the wardrobe dictionary.
  3. iterate the list.
wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}
for types,colors in wardrobe.items():
    for color in colors:
        print("{} {}".format(color,types))

Upvotes: 0

Rizwan Rao
Rizwan Rao

Reputation: 41

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

Upvotes: 4

Anonymous 286
Anonymous 286

Reputation: 57

You first use the items method to get the type and color with their corresponding values then iterate the colors for the respective type The code will make things more clear

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

Upvotes: 2

Deepan
Deepan

Reputation: 430

This may solve your problem

wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}
for i in wardrobe.keys():
    k=wardrobe[i]
    for j in k:
        print(j,i)

The output will be

red shirt
blue shirt
white shirt
blue jeans
black jeans

or

wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}
c=[j+' '+i for i in wardrobe for j in wardrobe[i]]
print(c)

Will give output in list format

Upvotes: 1

Related Questions