Reputation: 85
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
Reputation: 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.."
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
Reputation: 11
.items()
, to access the dictionary.wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}
for types,colors in wardrobe.items():
for color in colors:
print("{} {}".format(color,types))
Upvotes: 0
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
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
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