Copying in Python (WIP)

Motivation 💪🏻

Lets say, we are creating a list of ingredients for 🍋lemon tea and 🫚ginger tea. Trust me, the ☕tea and this example are both effective. Since both of them have common ingredients we proceed in this way:

  • Create a list named tea with all common ingredients
  • Copy the list to a new name called lemon_tea
  • Add lemon to the lemon_tea list
  • Copy tea to a new name called ginger_tea
  • Add ginger to the ginger_tea list

I have given the exact sequence of steps because, in the process of creating tea, we would end up in a spaghetti🍜

Lets do it in code 👉🏻

tea = ["Milk", "Sugar", "Tea Powder"]

# Lemon Tea
lemon_tea = tea
lemon_tea.append("Lemon")

# Ginger Tea
ginger_tea = tea
ginger_tea.append("Ginger")

# Print results
print(tea)
print(lemon_tea)
print(ginger_tea)
# Output to show from the code
return 0
['Milk', 'Sugar', 'Tea Powder', 'Lemon', 'Ginger']
['Milk', 'Sugar', 'Tea Powder', 'Lemon', 'Ginger']
['Milk', 'Sugar', 'Tea Powder', 'Lemon', 'Ginger']

Notice how insertions happened in all the lists. Now, that's a big mixup. Lets take a closer look why it happened