How do I efficiently append elements to a list in Python?

108 views

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
0
  • Use the append() method for adding single elements or extend() 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
0
You are viewing 1 out of 1 answers, click here to view all answers.