Python List append() Method with Examples

The list.append()method in Python is used to append an item to the end of a list. It modifies the original list in place and returns None (meaning no value/object is returned). The item being added can be of any data type, including a string, integer, or iterable like a dictionary, set, tuple, or even another list. When you append an iterable, it appends as a single element (for example appending a list results in a nested list).

Related:

In this article, I will explain the syntax of the python list append() method, its parameters and explain how to use it.

1. Quick Examples of List append() Function

If you are in a hurry, below are some quick examples of using the list append() function.


# Below are the quick examples

# Example 1: Append an item to the list
technology = ['Spark', 'Python', 'Pyspark']
technology.append('Hadoop')

# Example 2: Use append() function 
# Appends list to the list 
technology = ['Java', 'Spark', 'Python']
technology1 = ['Hyperion', 'Pandas', 'Pyspark']
technology.append(technology1)

# Example 3: Append tuple to the list 
technology = ['Java', 'Spark', 'Python']
technology1 = ('Hyperion', 'Pandas', 'Pyspark')
technology.append(technology1)

2. Syntax of list.append() Function

Following is a syntax of the append() function.


# Syntax of list.append() function
list.append(item)

2.1 Parameters of append()

  • item – The item to be appended to the list. It can be of any data type (e.g. string, integer, list, etc.).

2.2 Return Value

This function does not return any value but updates the existing list.

3. List append() Function Example

Use list.append() to append an item or element to a list, (element can be a string, list e.t.c). For example, if you have a list called technology and you want to append an element 'Hadoop' at the end, use this function. Here, is an example.


# Consider a list
technology = ['Spark', 'Python', 'Pyspark']
print("Actual List",technology)

# Add 'Hadoop' to the list
technology.append('Hadoop')
print("Updated List: ",technology)

This example yields the below output.

python list append

4. Append List using append() Function

You can also use the append() function to append another list to a list in Python. For example, the append() function is used to append list technology1 to the list technology. Now technology contains the elements of the original list and the new list, which is a nested list.


# Use append() function to add list to the list 
technology = ['Java', 'Spark', 'Python']
technology1 = ['Hyperion', 'Pandas', 'Pyspark']
technology.append(technology1)
print(technology)

# Output
# ['Java', 'Spark', 'Python', ['Hyperion', 'Pandas', 'Pyspark']]

Similarly, you can also append a list, set, tuple e.t.c to the list.

5. Other Ways to Append Element to List

5.1 Example 1

You can also use the list.extend() method to append the elements of one list to another. This will append the elements of the list to the end of the existing list. By using this you can append multiple elements to the list.


# Use extend() function to add list to the list 
technology = ['Java', 'Spark', 'Python']
technology1 = ['Hyperion', 'Pandas', 'Pyspark']

# Using extend
technology.extend(technology1)
print(technology)

# Output
# ['Java', 'Spark', 'Python', 'Hyperion', 'Pandas', 'Pyspark']

Similarly, if you want to add list to a single list, you can use for loop and append each element of the list to the existing list.


# Use extend() function
lists = [[3, 5], [4, 6], [2, 8]]
list1 = []
for lst in lists:
    list1.extend(lst)
print(list1)

# Output
# [3, 5, 4, 6, 2, 8]

5.2 Example 2

You can also use the += operator.


# use the += operator
technology = ['Spark', 'Python']
technology1 = ['Pandas', 'Pyspark']
technology += technology1
print(technology)

# Output
# ['Spark', 'Python', 'Pandas', 'Pyspark']

Alternatively, you can also use the + operator to append two lists. For example, you can create a new list that contains the elements of technology and 'Hyperion'.


# Use * operator
technology = ['Spark','Python','Pyspark']
technology = technology + ['Hyperion']
print(technology)

# Output
# ['Spark','Python','Pyspark','Hyperion']

5.3 Example 3

Use the list.insert() method to insert an element at a specific index in the list. For example, you can insert 'Java' at the first position of the list. For more examples, refer to add element to list by index.


# Use insert() function
technology = ['Spark','Python','Pyspark']
technology.insert(0, 'Java')
print(technology)

# Output
# ['Java', 'Spark', 'Python', 'Pyspark']

You can also use list comprehension to append an item to the list.


# use list comprehension
technology = ['Spark','Python','Pyspark']
technology = [x for x in technology] + ['Pandas']
print(technology)

# Output
# ['Spark', 'Python', 'Pyspark', 'Pandas']

Conclusion

In this article, I have explained the python list append() method syntax, parameters, and how to use it to append an element to the list at the end. Also, learned other ways to append elements.

Happy Learning !!

Leave a Reply

You are currently viewing Python List append() Method with Examples