View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Python Append Function: Why, What?(With Examples)

By Rohit Sharma

Updated on Sep 26, 2022 | 8 min read | 7.5k views

Share:

Python is a vast programming language with numerous applications and libraries. No wonder it is the most popular and widely used programming language by developers globally. What makes it a versatile language are its English-like syntax and the extensive range of functionalities that accompany it. 

One of those functions is the Python append function, which we’ll talk about today. You’ll learn what the append function is, why it’s so important, and what makes it unique. Learn data science to know more about various python functions. 

Why is the Python append function important?

The append function plays a crucial role in Python when you are working with lists. To understand how it works, you must know why it’s so important and get familiar with the objects it interacts with.

We use the Python append function with lists. They are pretty similar to the arrays and don’t necessarily have to be of the same data type. That’s why lists are among the most popular and robust tools in Python. The primary difference between lists and arrays lies in the former’s ability to use multiple data types.

A list can have integers, strings, and other lists in itself, and that’s not possible with arrays. Moreover, lists are mutable. In other words, you can modify a list even after you have created it.

Python lists are indexed and have a definite count during the initialization process. The system indexes the elements present in a list according to a fixed sequence. The indexing process starts with 0 being the first index while the last item’s index is n-1, where n stands for the number of items present in the list.

Every element in a list has an indexed place. This feature allows you to duplicate the elements present in the list as you can repeat an element in the list because both of them would have a different index to distinguish between them. This feature is not present in sets, which is why lists are widely popular in Python implementations.

We create lists in python by utilizing [] brackets. You’d have to use commas to separate the elements present in the list. Here’s an example of a list in Python:

Input: 
first_list = [‘upGrad’, ‘is’, ‘fun’]
print first_list
Output:
[‘upGrad’, ‘is’, ‘fun’]
In the above list, the index of the item ‘upGrad’ is 0. You can use the len() function to find the index of a list:
Input: 
len(first_list)
Output: 

3

Certainly, lists are versatile objects with numerous potential applications.

However, if you wanted to add an item to a list after its creation, what would you do? This is where the Python list append function comes in. Let’s understand that in detail in the next section. 

What is the Python append function?

The append function adds its argument to a list. More precisely, it adds a single element to the end of a specific list and increases the length of the same by one. 

The append function allows you to add an item to the end of the list. It adds the element regardless of its data type. Hence, you can add a string, number, or even another list to the end of the particular list by using this function.

The syntax of this function is: list_name.append(element)

The input parameter of the append function is one item, and it would add it to the end of a particular list. Also, because it only adds an item to an existing list, it doesn’t have any return value.

Examples of Python list append

Here are a few examples of how we use the Python list append function with different data types:

1. Adding a number to a list with append function

Here’s how we add a number to a list by using this function:

Input: 
# create a list of strings
first_list = [‘upGrad’, ‘is’, ‘fun’]
# add an integer item to first_list
first_list.append(1)
# print the new list
print(first_list)
Output:

[‘upGrad’, ‘is’, ‘fun’, ‘1’]

2. Adding a string to a list with append function

You can also use the append function to add a string to a list:

Input: 
# create a list of strings
first_list = [‘upGrad’, ‘is’]
# add a string item to first_list
first_list.append(‘fun’)
# print the new list
print(first_list)
Output:

[‘upGrad’, ‘is’, ‘fun’]

3. Adding a list to a list with append function

The append function can add a list to another list as an element:

Input: 
# create a list of strings
# and another list to append to the first one
first_list = [‘upGrad’, ‘is’, ‘fun’]
second_list = [1, 2, 3, 4]
# append second_list to first_list
first_list.append(second_list)
# print the new list
print(first_list)
Output:
[‘upGrad’, ‘is’, ‘fun’, [1, 2, 3, 4]]

Append vs. extend functions 

A common point of confusion among Python beginners is about the difference between the append function and the extend function. Both of them extend the length of a list however the main difference lies in the number of elements they add to a list at a time.

The append function adds one item to the end of a list. On the other hand, the extend function iterates over the argument and adds every element of its list to the specific list. With the append function, the index of a list increases by one but with the extend function, it increases by the number of elements present in the function’s argument.

The append function works with various data types, but the extend function works with lists only. Here’s a pair of examples to explain their difference better. In the first case, we will use the extend function with first_list and second_list, while in the second case, we’ll apply the append function:

Input: 
# We’ll use the extend function
# each element of the iterable will be appended
# to first_list
first_list = [‘upGrad’, ‘is’]
second_list = [1, 2, 3, 4]
first_list.extend(second_list)
print first_list
Output: 
[‘upgrad’, ‘is’, 1, 2, 3, 4]
Input: 
# Here, we’ll use the append function
# the list will be added as an element
# to first_list
first_list = [‘upGrad’, ‘is’]
second_list = [1, 2, 3, 4]
first_list.append(second_list)
print first_list
Output:

[‘upGrad’, ‘is’, [1, 2, 3, 4]]

As you can see, the extend function added the elements present in second_list to first_list, and every item present in second_list became the element of first_list. On the other hand, the append function added second_list as an element to first_list.

With the extend function, the length of first_list increased by four elements. However, the append function increased the length of first_list only by one.

This is the primary difference between the Python append function and the extend function.

Become an expert in Python and related technologies

The append function is one of the many functionalities present in Python. If you’re interested in becoming a Python expert, you’d need to familiarize yourself with the various functions and objects it works with. There are many applications of this programming language as it houses one of the largest collections of libraries and frameworks. Numpy, Scipy, and OpenCV are some of the most popular Python libraries.

For example, if you pursue a career in machine learning and artificial intelligence, then you’ll enjoy using OpenCV Keras and Scikit-learn. On the other hand, if you’re a data science professional, then you’d need to learn in detail about Numpy, Scipy, TensorFlow, Scikit-learn, Pandas, and several other libraries.

At upGrad, we offer multiple programs to help you become an expert in this programming language and your desired tech field. If you want to become an expert in Python and use this robust language to solve business problems and drive your career, then we recommend checking out our Master of Science in Computer Science program we offer in partnership with Liverpool John Moores University.

It’s an online degree course that lasts for 19 months and only requires a time commitment of 15 hours a week. The program teaches you the in-demand skills of the software development field and certifies you as an expert.

More about the course: 

The Master of Science in Computer Science program offers you 500+ learning hours and gives you the alumni status of Liverpool John Moores University and International Institute of Information Technology Bangalore.

You’ll be joining a learner base of more than 40,000 students in over 85 countries across the globe. The program allows you to network globally and boost your professional growth.

The MSc program offers six unique specializations:

  1. Full-stack development
  2. Blockchain development
  3. Big data
  4. Cloud computing
  5. DevOps
  6. Cyber Security

If you have a non-tech background, you can join the Software Career Transition Bootcamp for non-tech & new coders. There’ll be fortnightly group mentorship sessions with industry mentors and 30+ projects and assignments, so you can test your knowledge and enhance your understanding of the concepts.

Learn more about the course here: Master of Science in Computer Science.

Conclusion

Today we learned about the Python list append function. The append function allows you to add an item to the end of the list and is quite useful in many situations. We also learned how the append function is different from the extend function. 

If you want to learn more about Python and become a tech professional, be sure to check out the course we shared above.

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

Frequently Asked Questions (FAQs)

1. What is the Python append function?

2. How do we use the Python append function?

3. How many items does the append function add to a list?

Rohit Sharma

690 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program