1
colours = ["red", "green", "blue"]
clothes = ["shirt", "dress", "pants", "jacket", "hat"]


for colour_item in colours:
    for clothes_item in clothes:
        print("I am wearing a ",colour_item," ",clothes_item)

This is the code i am trying to change into while loops to produce all outcomes, ie 15 outcomes, the best i can get with while loops is 3.

2
  • 1
    Why would you want to use a while loop for this? Commented Aug 14, 2021 at 2:13
  • Include your while loop code. Commented Aug 14, 2021 at 2:16

3 Answers 3

1

You can try using a while loop while also keeping an index counter variable for each list, though it is essentially just a for loop:

colours = ["red", "green", "blue"]
clothes = ["shirt", "dress", "pants", "jacket", "hat"]

colorIndex = 0
while(colorIndex < len(colours)):
  clothesIndex = 0
  while(clothesIndex < len(clothes)):
    print("I am wearing a",colours[colorIndex],clothes[clothesIndex])
    clothesIndex += 1
  colorIndex += 1

Output:

I am wearing a red shirt
I am wearing a red dress
I am wearing a red pants
I am wearing a red jacket
I am wearing a red hat
I am wearing a green shirt
I am wearing a green dress
I am wearing a green pants
I am wearing a green jacket
I am wearing a green hat
I am wearing a blue shirt
I am wearing a blue dress
I am wearing a blue pants
I am wearing a blue jacket
I am wearing a blue hat
Sign up to request clarification or add additional context in comments.

Comments

1

If you are willing to do a little math, you can do this in a single while loop.

colours = ["red", "green", "blue"]
clothes = ["shirt", "dress", "pants", "jacket", "hat"]

n = 0
l = len(clothes)

while n < len(colours) * len(clothes):
    print(f"I am wearing a {colours[n // l]}  {clothes[n % l]}")
    n += 1

Which prints the expected:

I am wearing a red  shirt
I am wearing a red  dress
I am wearing a red  pants
I am wearing a red  jacket
I am wearing a red  hat
I am wearing a green  shirt
I am wearing a green  dress
I am wearing a green  pants
I am wearing a green  jacket
I am wearing a green  hat
I am wearing a blue  shirt
I am wearing a blue  dress
I am wearing a blue  pants
I am wearing a blue  jacket
I am wearing a blue  hat

5 Comments

I don't think this is more efficient though, it's still O(n^2)
@AnikethMalyala considering the OP wants to print m * n items O(n * m) seems impossible to beat. This is expressed pretty clearly in the loop len(colours) * len(clothes)
O(m*n), my bad. I thought you were implying that your singular while loop was more efficient than nested ones, my bad!
@AnikethMalyala I can see how it seemed that way. But no, just making the code shorter (but maybe not more clear). Personally, I think for loops or itertools.product are the correct way to do this.
Fr, using a for each loop seams much better in this case
0
colours = ["red", "green", "blue"]
clothes = ["shirt", "dress", "pants", "jacket", "hat"]

i=0
while i<len(colours):
    j=0
    colour_item = colours[i]
    while j<len(clothes):
        clothes_item = clothes[j]
        print("I am wearing a ",colour_item," ",clothes_item)
        j+=1
    i+=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.