How to use Append Python Function
append python function adds an element at the end of the existing list. In this article, we will learn how to use append function in python with the help of several examples.
Python append function adds an element at the end of the list. The append function returns nothing; it only modifies the existing list. In this article, we will discuss the append python function with the help of an example.
Later in the article, we will discuss the difference between append() and extend() functions in Python.
Also Read: Python Function
Also Read: Introduction to Python β Feature, Use Cases, Resource, and Applications
Must Check: Top Python Online Course and Certifications
Table of Content
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is Append in Python?
append() is a built-in python function that adds a single element at the end of the existing list.
- Every time append () is called on an existing list, the method adds a new item to the end of the list.
- The append () function doesnβt return anything; it only modifies the existing list.
Syntax
my_list.append(item)
Note:
- The item can be an integer, floating number, boolean, string, list, user-defined object, etc.
- The parameter (item) is mandatory.
- If you leave the parameter, it will throw an error.
Example 1:
#using append() functionmy_name = ['Infoedge', 'Naukri']my_name.append('Learning')my_name
Output
Example 2:
#using append() functionmy_name = ['Infoedge:', 'Shiksha Online']my_name.append('Siksha')my_name.append('Naukri')my_name.append('Jeevansathi')my_name.append(99)
#updated list after adding element using append() function.my_name
Output
Time Complexity
The time complexity of the append () function is constant, i.e., O (1). As lists are randomly accessed so the last element can be accessed in O (1) time.
How to append ()
- List in Python
A list is an object; if you append a list to another list, it will be treated as a single object.
Example 3
#python list append
name = ['Vikram', 'Vikas']dob_month = [9, 12]name.append(dob_month)name
Output
Example 4: Nested List
#nested python list append()
#create a listlist_1 = [2, 4, 6]list_2 = [3, 6, 9]list_3 = [4, 8, 12]
#first append list_2 in list_1 and then append list_3 in list_2
list_1.append(list_2)list_2.append(list_3)list_1
Output
Also Read: List in Python
- Array in Python
Python arrays are linear-ordered data structures that store the elements of the same data type at contiguous memory locations.
Example 5
#python array append#import arrayimport array as arr
#define variable/create Array
grade = arr.array('d', [7.4, 8.6, 6.8, 9.4, 8.4])
grade.append(8.8)grade
Output
Also Read: How to use Python Arrays
- Dictionary in Python
Dictionaries are likely the most important and flexible mutable built-in data types in Python. In simple terms, a Python dictionary is a flexible sized arbitrary collection of key-value pairs, where key and value both are Python objects.
Example 6
#python dictionary appendmy_list = [1,2,3, 4]my_dict = {'name':'Vikram', 'age': 22, 'sex': 'male'}my_list.append(my_dict)my_list
Output
Also Read: Introduction to Python Dictionary
Also Read: Python Dictionary Practice Programs for Beginners
Difference between append () and extend ()
- append ()
- Element in append () function passed as an argument will be added at the end of the list.
- The length of the list is increased by 1
- Time Complexity: O (1)
- Example
#example of append() function in python
car = ['Hyundai', 'Ford', 'Mahindra']bike = ['BMW', 'KTM', 'Yamaha']car.append(bike)car
Output
- extend ()
- Each element of the iterable passed as an argument gets added at the end of the list.
- The list length is increased by the number of elements in the iterable.
- Time Complexity: O(n), where n is the length of the iterable
- Example
#example of extend() function in python
car = ['Hyundai', 'Ford', 'Mahindra']bike = ['BMW', 'KTM', 'Yamaha']car.extend (bike)car
Output
Conclusion
In this article, we have briefly discussed how to use append function in python.
Hope, you will like the article.
Keep Learning!!
Keep Sharing!!