To append an element to a list in Python, you can use the append
method of list object.
In this example,
numbers
with initial integer values.append
method of the list numbers
to add a new integer to the list.append
method modifies the original list by adding the new element at the end.numbers = [1, 2, 3, 4]
numbers.append(5)
print("Updated list:", numbers)
Updated list: [1, 2, 3, 4, 5]
In this example,
numbers
with initial integer values.new_elements
containing elements to be appended.new_elements
and append each one to numbers
using the append
method.numbers = [1, 2, 3, 4]
new_elements = [5, 6, 7]
for elem in new_elements:
numbers.append(elem)
print("Updated list:", numbers)
Updated list: [1, 2, 3, 4, 5, 6, 7]
In this tutorial, we learned How to Append an Element to a List in Python language with well detailed examples.