Difference Between append and extend in Python
append and extend functions in Python are used to add elements at the end of the list. This article will discuss the difference between append and extend in Python.
Table of Content
- Difference Between append and Extend in Python: append vs. extend
- What is append in Python?
- What is extend in Python?
- Key Difference Between append and extend in Python
What is the difference Between append and extend in Python?
append() | extend() | |
Functionality | adds a single element at the end of the list. | add multiple elements (from an iterable) to the end of the list. |
Argument Type | Accept a single element as an argument. | Accept an iterable (like a list, tuple, or string) as an argument. |
Operation | Adds the full input to the list as an argument. | Adds each element to the list independently after iterating through each one in the input. |
Syntax | list_name.append(element) | list_name.extend(iterable) |
Efficiency | It is typically quicker and more efficient than extend, operating only one operation at a time. | It adds elements from iterable or with huge inputs that may take longer time. |
Time Complexity | It has constant time complexity, O(1). | It has a time complexity of O(k). Where k is the length of the list which need to be added. |
Impact on Original List | Modifies the original list in place. | Modifies the original list in place. |
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is append in Python?
The append function is a built-in function in Python that adds a single element at the end of the list. It is part of the list class and can be invoked using the dot notation.
It is an in-place method, meaning it modifies the original lists and does not return any value.
Syntax of append function in Python:
list.append(element)
Letโs take an example to get a better understanding of how append function in function works:
#initialize a listmy_list = [1, 2, 3, 4, 5]
#add an element 6 at the end of the my_listmy_list.append(6)
#printing the outputprint(my_list)
Output
[1,2,3,4,5,6]
Until now, you have a clear understanding of what append function is and how it work. Now, itโs time for some practical based example.
Discover the key to career success with top courses after 12th. Further, navigate through specialized online degree programs for career excellence.
Example-1: Data Collection in Machine Learning
Simulate a data collection process that collects features of different samples and append them to a list which will be used to create a dataset later.
# Initializing an empty list to store sample featuresdata_samples = []
# Simulating data collection of features for multiple samplesfor i in range(5): # Creating a dictionary to store features of a sample sample_features = { "feature1": i, "feature2": i * 2, "feature3": i + 3 } # Using append function to add the sample features dictionary to the data samples list data_samples.append(sample_features)
# Printing the data samples list# Output: A list of dictionaries containing features of individual samplesprint(data_samples)
Output
[{'feature1': 0, 'feature2': 0, 'feature3': 3}, {'feature1': 1, 'feature2': 2, 'feature3': 4}, {'feature1': 2, 'feature2': 4, 'feature3': 5}, {'feature1': 3, 'feature2': 6, 'feature3': 6}, {'feature1': 4, 'feature2': 8, 'feature3': 7}]
Example-2: Real time Data Analysis
Simulate a real-time data analysis scenario where data points are collected over time and append them to a list for further analysis.
# Initializing an empty list to store real-time data pointsreal_time_data = []
# Simulating real-time data collection over a period of time (e.g., sensor readings)for time_stamp in range(0, 100, 10): # Simulating a sensor reading (for simplicity, using time_stamp as the sensor reading) sensor_reading = time_stamp # Using append function to add the sensor reading to the real-time data list real_time_data.append(sensor_reading)
# Printing the real-time data list# Output: A list of sensor readings collected over timeprint(real_time_data)
Output
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
What is extend in Python?
Similar to append, extend is also a built-in function used with a list to add the element at the end of the list. But unlike append, it does not take a single element. extend function in Python takes an iterable (like a list, tuple, or string) as an argument and adds each element of the iterable to the list.
extend method is useful when you want to merge a list with another iterable. It iterates over its argument and does not create a new list, which helps in saving memory while working with large lists.
Syntax for extend function in Python
list.extend(iterable)
Letโs take an example of the extend function to get more idea of how it works.
# Initializing a listmy_list = [1, 2, 3]
# Using the extend method to add multiple elements (4, 5, and 6) to the end of the listmy_list.extend([4, 5, 6])
# Printing the modified listprint(my_list)
Output
[1,2,3,4,5,6]
Until now, you have a clear understanding of what extend function is and how it work. Now, itโs time for some practical based example.
Example-1: Merging Datasets in Data Science
Use the extend function to merge two lists representing the datasets.
# Initializing two separate lists representing two datasetsdataset1 = [{"id": 1, "value": 100}, {"id": 2, "value": 200}]dataset2 = [{"id": 3, "value": 300}, {"id": 4, "value": 400}]
# Using the extend function to merge dataset2 into dataset1dataset1.extend(dataset2)
# Printing the merged dataset# Output: A merged list of dictionaries representing a combined datasetprint(dataset1)
Output
[{'id': 1, 'value': 100}, {'id': 2, 'value': 200}, {'id': 3, 'value': 300}, {'id': 4, 'value': 400}]
Example-2: Feature Engineering in Machine Learning
Add new features to the existing feature set during the model training.
# Initializing a list representing the initial feature setfeature_set = ["feature1", "feature2", "feature3"]
# Creating a list of new features that we want to add to our feature setnew_features = ["feature4", "feature5"]
# Using the extend function to add the new features to the feature setfeature_set.extend(new_features)
# Printing the updated feature set# Output: A list representing the updated feature set with new features addedprint(feature_set)
Output
['feature1', 'feature2', 'feature3', 'feature4', 'feature5']
What is the Key Difference Between append and extend in Python?
- Both methods modify the original list in place without creating a new list.
- Both return NONE (just modifies the list).
- Append function in Python adds a single element to the end of the list, whereas the extend function adds multiple elements (from an iterable) to the list.
- Append takes a single element as an argument, while extend takes an iterable as an argument.
- The append function is more efficient than the extend function as it operates only operations at a time.
- The complexity of append is O(1), while the complexity of extend function is O(k).
- Where k is the length of the list that need to be added.