How to Loop a string list?

I’m brand new to coding and have been teaching myself Python. I have been trying to create multiple shopping list, that I can print out in a f-string. My code keeps repeating the f-string by the number of items in the list. No matter what I try, I can’t get the list of items to print.

I would like to be able to create individual shopping list. that I can add and take away items from, that automatically update the f-string when printed.

This is what I have:

Dads_shopping_list = ["Body Wash", "Soda", "Water", "Candy Bar"]

Izaiahs_shopping_list = ["Apples", "Wings", "Chips"]

for Dad in Dads_shopping_list:
    for Izaiah in Izaiahs_shopping_list:
        print(f"Hey can you pick up [Dad] for Dad. And can you bring me back [Izaiahs].")

  • do you want to make it delete the items after priniting it ?

    – 

  • What output do you want exactly, and what are you getting currently? I just fixed a typo in your code where you had a curly quote instead of a regular quote, but you’re not reporting a syntax error so obviously that wasn’t a problem, but I’m not sure if the square brackets instead of braces are also a typo. And I’m not sure what the output is supposed to look like anyway.

    – 

  • Oh and welcome to Stack Overflow! Please take the tour. Check out How to Ask for tips. You can edit your posts to clarify.

    – 




  • In lieu of clarification, my best guess is you want the output to be Hey can you pick up Body Wash, Soda, Water, Candy Bar for Dad. And can you bring me back Apples, Wings, Chips., in which case there are existing questions about the same thing, e.g. How would you make a comma-separated string from a list of strings?

    – 

  • Please add a sample of the output you are trying to get.

    – 

This is what zip is for. It iterates multiple iterables at once:

for Dad, Izaiah in zip(Dads_shopping_list, Izaiahs_shopping_list):
    print(f"Hey can you pick up {Dad} for Dad. And can you bring me back {Izaiahs}.")

I assume that you need the output like ‘Hey can you pick up -,-,-,dad list items,-,- for Dad. And can you bring me back -,-,-, Izaiah list items,-,- ‘. You can try the below code by joining the list.

Dads_shopping_list = ["Body Wash", "Soda", "Water", "Candy Bar"]
Izaiahs_shopping_list = ["Apples", "Wings", "Chips"]

print("Hey can you pick up " + ','.join(Dads_shopping_list)+ " for Dad. And can you bring me back " +  ','.join(Izaiahs_shopping_list) + ".")

output

Hey can you pick up Body Wash,Soda,Water,Candy Bar for Dad. And can you bring me back Apples,Wings,Chips.

Leave a Comment