How to use Append Python Function

How to use Append Python Function

2 mins read1.5K Views Comment
Updated on Aug 27, 2024 17:47 IST

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.

2022_11_MicrosoftTeams-image-77.jpg

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

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
β‚Ή3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks
– / –
3 months

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)
Copy code

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() function
my_name = ['Infoedge', 'Naukri']
my_name.append('Learning')
my_name
Copy code

Output

Example 2:


 
#using append() function
my_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
Copy code

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.

Introduction to Python Range Function
Python Map() Function
Python Sorted() Function

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
Copy code

Output

Example 4: Nested List


 
#nested python list append()
#create a list
list_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
Copy code

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 array
import 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
Copy code

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 append
my_list = [1,2,3, 4]
my_dict = {'name':'Vikram', 'age': 22, 'sex': 'male'}
my_list.append(my_dict)
my_list
Copy code

Output

Also Read: Introduction to Python Dictionary

Also Read: Python Dictionary Practice Programs for Beginners

Difference between append () and extend ()

  • append ()
  1. Element in append () function passed as an argument will be added at the end of the list.
  2. The length of the list is increased by 1
  3. Time Complexity: O (1)
  4. Example

 
#example of append() function in python
car = ['Hyundai', 'Ford', 'Mahindra']
bike = ['BMW', 'KTM', 'Yamaha']
car.append(bike)
car
Copy code

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
Copy code

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!!

About the Author