How do I efficiently append elements to a list in Python?
How do I efficiently append elements to a list in Python?
How do I efficiently append elements to a list in Python?
solveurit24@gmail.com Changed status to publish February 13, 2025
- Use the
append()method for adding single elements orextend()for adding multiple elements from an iterable.
my_list = [1, 2, 3]
my_list.append(4) # Adds 4 to the list
my_list.extend([5, 6]) # Adds 5 and 6 to the list
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
solveurit24@gmail.com Changed status to publish February 13, 2025