Skip to content

Instantly share code, notes, and snippets.

@RodolfoFerro
Created November 9, 2017 19:47
Show Gist options
  • Save RodolfoFerro/161ae8fd91ed1d8f1e8ae28648d4540d to your computer and use it in GitHub Desktop.
Save RodolfoFerro/161ae8fd91ed1d8f1e8ae28648d4540d to your computer and use it in GitHub Desktop.
Gist to illustrate the insertion of first element on nested list.
# Import copy lib
import copy
# Create lists:
names = ["Mary", "David", "George"]
numbers = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
# Create a deep copy to avoid using same memory direction:
new_list = copy.deepcopy(numbers)
# Insert elements as wanted:
for i in new_list:
i.insert(0, names[0])
# Print (the one edited) new list:
print(new_list)
# We can compare with the original list:
print(numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment