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.
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.
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)
Adding another list to end of List
The extend() method will Iterates over its argument and adding each element to the list and extending 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.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.