In this Python Article, we will understand the difference between Python Extend and Python Append functions.
Before diving into the details of these two methods, let’s clarify what a list is in Python.
A list is a type of mutable sequence in Python. It is commonly used to store collections of homogeneous items, though it can store different types of items.
Python Append() Method
The append() method in Python adds an element to the end of a list.
Here’s an example:
# create an empty list us_cities =  # append elements to the list us_cities.append('New York') us_cities.append('Los Angeles') print(us_cities)
In this example, we added the names ‘New York’ and ‘Los Angeles’ to our list one by one using the Python append() method.
Notably, if you append a Python list to another list, the entire list gets appended as a single element (a sublist).
us_cities = ['New York', 'Los Angeles'] us_cities.append(['Chicago', 'Houston']) print(us_cities)
As you can see, the list [‘Chicago’, ‘Houston’] was added as a single element to us_cities, not as individual elements.
Python Extend() Method
The Python extend() method, on the other hand, adds all elements from an iterable (like a list, tuple, string etc.) to the end of a list.
Here’s how it’s used:
# create a list us_cities = ['New York', 'Los Angeles'] # extend the list with more elements us_cities.extend(['Chicago', 'Houston']) print(us_cities)
In this example, we extended the Python list with two new city names, ‘Chicago’ and ‘Houston’. These were added as individual elements, not as a sublist.
Extend() vs Append() in Python
|Python Append() Function||Python Extend() Function|
|Python append() ddds its argument as a single element to the end of a list.||Python extend() adds each of the elements from the iterable argument to the end of the list.|
|Python append() function helps in adding single items or nested lists.||Python extend() function helps in adding multiple items from an iterable.|
|This function is used to change the original list.||The append() function accepts any data type.|
|The extend() function only accepts iterables.||The extend() fucntion only accepts iterables.|
|Python append() function can result in nested lists.||Python extend() function always results in flat lists.|
|Python append() function works faster for appending single items.||Python extend() function Faster for appending multiple items from iterables.|
|The append() function in Python is more memory-efficient when appending single items.||The extend() function in Python is more memory-efficient when extending with iterables.|
Comparison and Use Cases of Python Append() vs Extend()
- Appending single elements: If you only need to add a single element to a Python list, using append() is the appropriate method.
- Adding multiple individual elements from an iterable: If you have an iterable and you want each of its elements to be new elements in the existing Python list, use extend().
- Adding a list as a sublist: If you have a Python list that you want to add as a sublist to an existing list, use append().
In conclusion, both append() and extend() methods are very useful when dealing with Python lists. Understanding the nuances of how they work can help optimize your code and avoid unexpected results.
Always keep in mind the type of argument you’re adding and the structure you want for your list to choose the right method.
You may also like to read the following Python tutorials.
- How to Python Append List to Another List
- How to find perfect number in Python
- How to reverse a number in Python
- Sum Elements in List in Python using For Loop
- Append multiple items to list Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.