duplicate a list with for loop in python 1

duplicate a list with for loop in python

her_clothes = ["women's trousers", "women's shirts", "women's shoes", "women's sunglasses"]
his_clothes = []

for clothes in her_clothes:
    his_clothes.append(clothes.replace("women's", "men's"))

print(his_clothes)

# output - ["men's trousers", "men's shirts", "men's shoes", "men's sunglasses"]

Here is what the above code is Doing:
1. We create a list called her_clothes and add four items to it.
2. We create an empty list called his_clothes.
3. We use a for loop to iterate over the items in her_clothes.
4. For each item in her_clothes, we use the replace() method to replace “women’s” with “men’s”.
5. We add the modified item to his_clothes.
6. We print his_clothes.

Similar Posts