Table of contents
1.
Introduction
2.
What is append() in Python?
3.
Python append() Syntax
4.
Examples of Append Method in Python
4.1.
Python
4.2.
Python
4.3.
Python
5.
Use of Append in Python 
6.
Advantage of the append() Method
7.
Frequently Asked Questions
7.1.
What does append() return in Python?
7.2.
What is append in List_name?
7.3.
What are the benefits of append()?
8.
Conclusion
Last Updated: Nov 13, 2024
Easy

Append in Python

Author Juhi Sinha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The append() method in python adds a single item to the existing list. It doesn’t return a new list of items but will modify the original list by adding the item to the end of the list.

Learn to append in Python

Every programming language has a certain number of built-in data structures. Data structures are basically structures that hold the data together so that we can use it to our advantage.

What is append() in Python?

In Python, the append() method is used to add a single element to the end of an existing list. It modifies the original list in place, making it an efficient and commonly used method to add new items to lists. This method is particularly useful when you want to add elements dynamically during runtime without creating a new list.

Python append() Syntax

The syntax of the append() method is straightforward:

list.append(element)
  • list: This is the list to which you want to add the element.
  • element: This is the item you want to append to the list. The element can be of any data type—string, integer, float, list, dictionary, etc.

append() does not return a new list; it modifies the existing list by adding the element to its end.

Examples of Append Method in Python

Here are the examples of using append() with their outputs:

1. Appending numbers in a loop:

  • Python

Python

squares = []
for i in range(1, 5):
squares.append(i ** 2)
print(squares)
You can also try this code with Online Python Compiler
Run Code

Output:

[1, 4, 9, 16]

 

2. Building a list of student records:

  • Python

Python

students = []
students.append({"name": "Rahul", "grade": 95})
students.append({"name": "Rohit", "grade": 87})
students.append({"name": "Virat", "grade": 92})
print(students)
You can also try this code with Online Python Compiler
Run Code

Output:

[{'name': 'Rahul', 'grade': 95}, {'name': 'Rohit', 'grade': 87}, {'name': 'Virat', 'grade': 92}]

 

3. Creating a list of words from a sentence:

  • Python

Python

sentence = "Python is awesome"
words = []
for word in sentence.split():
words.append(word.upper())
print(words)
You can also try this code with Online Python Compiler
Run Code

Output:

['PYTHON', 'IS', 'AWESOME']

Use of Append in Python 

The append method is used in several common scenarios:

1. Building lists dynamically: 

user_inputs = []
user_inputs.append("first input")
user_inputs.append("second input")

 

2. Collecting results:

results = []
for number in range(5):
   if number % 2 == 0:
       results.append(number)
# results will contain even numbers: [0, 2, 4]

 

3. Storing processed data:

names = ["rahul", "rohit", "virat"]
capitalized = []
for name in names:
   capitalized.append(name.capitalize())
# capitalized will be: ["Rahul", "Rohit", "Virat"]

Advantage of the append() Method

The main advantage of the append() method is its efficiency and ease of use:

  • In-place Modification: append() modifies the original list in place, so there’s no need to create a new list each time an item is added.
  • Dynamic: append() allows for adding elements to lists dynamically, making it suitable for cases where the number of items may vary.
  • Efficient Memory Management: As append() adds to the existing list rather than creating a new one, it is memory-efficient, especially for large datasets.

You can also read about the Multilevel Inheritance in Python and Convert String to List Python.

Check out Escape Sequence in Python

Frequently Asked Questions

What does append() return in Python?

The append() method in Python returns None. It modifies the original list by adding an element to its end without creating or returning a new list.

What is append in List_name?

In Python, List_name.append() means invoking the append() method on List_name, which is a list. It adds the specified element to the end of the list, modifying it in place.

What are the benefits of append()?

append() is efficient and memory-friendly, modifying lists directly without copying. It enables dynamic list-building, ideal for runtime data collection, and helps create lists efficiently with minimal code.

Conclusion

We have seen the append in Python in detail. We have also learnt four built-in structures in Python. I hope you have liked this article.

You can check out our courses on Web development if you wish to build a few projects on your own under the guidance of our Mentors. Also, refer to our Guided Path on Code360 to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, and many more! If you wish to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

Also check out - Data Dictionary In Software Engineering

Happy Learning!

Live masterclass