Python 3 – List append() Method
Lists are an important data type in Python programming language. They are ordered collections of elements, which can be manipulated easily with built-in functions and methods. One of the most commonly used methods for adding elements to a list is the append() method. The append() method is a built-in function in Python 3, which allows us to add new elements to the end of a list. In this article, we will learn how to use the append() method and how it works in Python.
Syntax of append() method
The syntax of the append() method is simple and easy to remember. It takes only one parameter, which is the element we want to add to the list. The syntax of the append() method is as follows:
list.append(element)
Here, the “list” is the name of the list we want to modify, and “element” is the item we want to add to the end of the list. We do not have to specify the index of the new item as the append() method always adds the new element to the end of the list.
Examples of append() method
Let’s take a look at some examples of using the append() method to add elements to a list.
# Example 1: Adding an integer to a list
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
# Output: [1, 2, 3, 4]
# Example 2: Adding a string to a list
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
print(fruits)
# Output: ['apple', 'banana', 'cherry', 'orange']
# Example 3: Adding a list to a list
languages = ['Python', 'Java']
languages.append(['C', 'C++'])
print(languages)
# Output: ['Python', 'Java', ['C', 'C++']]
In the first example, we added an integer “4” to the “numbers” list using the append() method. In the second example, we added a string “orange” to the “fruits” list. In the third example, we added another list “[‘C’, ‘C++’]” to the “languages” list. We can see that the append() method adds the new elements to the end of the list, regardless of the type of the element.
Using append() method inside a for loop
The append() method can be used inside a for loop to add multiple elements to a list. For example, we can use a for loop to iterate over a range of numbers and add them to a list using the append() method.
# Example: Adding numbers 1 to 10 to a list
numbers = []
for i in range(1, 11):
numbers.append(i)
print(numbers)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In this example, we created an empty list called “numbers” and used a for loop to iterate over the range of numbers 1 to 10. Inside the for loop, we used the append() method to add the number to the “numbers” list. Finally, we printed the list to verify that all the numbers were added correctly.
Using the + operator to concatenate lists
While the append() method is useful for adding single elements to a list, we can use the + operator to concatenate two or more lists into a single list. The + operator adds the elements of one list to the end of another list. Here’s an example:
# Example: Concatenating two lists
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = list1 + list2
print(list3)
# Output: [1, 2, 3, 'a', 'b', 'c']
In this example, we used the + operator to concatenate the “list1” and “list2” lists into a new list called “list3”. The result was a single list containing all the elements of both lists.
Conclusion
In this article, we learned about the append() method in Python 3. We saw how to use the append() method to add elements to a list, and how to use it inside a for loop. We also learned how to concatenate lists using the + operator. The append() method is a powerful tool for working with lists in Python, and it makes it easy to add new elements to a list without having to worry about indexing or other complex operations.