Skip to content

Add to end of list Python | Example code

  • by

Use the append() method to add a new element at the end of the list in Python. The append () method will add the single element to the end of the list.

Note: it does not return the new list, just modifies the original.

list.append(elem)

If you want to add the list to the end of the list in Python then use extend() method. It will shift elements to the right.

list.extend(list2)

Example add to the end of list in Python

Simple example code.

Adding a single element to the end of List

It will add a single object at the end of the list.

lst = ['A', 'B', 'C']

lst.append('D')

print(lst)

Output:

Add to end of list Python

Adding another list to the end of List

The extend() method will Iterates over its argument and add each element to the list and extend the list.

lst = ['A', 'B', 'C']
lst2 = [1, 2, 3]

lst.extend(lst2)

print(lst)

Output: [‘A’, ‘B’, ‘C’, 1, 2, 3]

Must Read: Combine two lists in Python

Do comment if you have any doubts and suggestions on this Python List topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *