In this article, learn more about Python’s .append() method. It also explains how .append() differs from other methods used to add items to a list.
Let’s start!
What are lists in Python? A definition for a new starter.
An array in programming is an ordered collection of elements, all of which must be of the same data type.
However, unlike other programming languages, arrays are not Python’s built-in data structures. Python uses lists instead of traditional arrays.
Lists, which are basically dynamic arrays, are one of the most common and powerful data structures in Python.
You can think of these as ordered bins. They store and organize similarly related data together.
The items stored in the list can be of any data type.
There can be lists of integers (integers), lists of floats (floating point numbers), lists of strings (text), and lists of other built-in Python data types.
Lists can only contain elements of the same data type but are more flexible than traditional arrays. This means that you can have many different data types in the same list. A list has zero or more elements. That is, there can also be empty lists. There may be duplicate values in the list.
Separate the values with commas and enclose them in square brackets [].
How to create lists in Python
To create a new list, first, give the list a name. Then add an assignment operator (=) and a pair of left and right brackets. In brackets, add the values you want to include in your list.
#create a new list of names
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
#print the list to the console
print(names)
#output
#['Jimmy', 'Timmy', 'Kenny', 'Lenny']
How lists are indexed in Python
The list maintains the order of each item.
Each item in the collection has its own index number, which can be used to access the item itself.
Indexes in Python (and other modern programming languages) start at 0 and increment for each item in the list.
For example, the list we created earlier had four values.
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
The first value in the list, “Jimmy”, has an index of 0.
The second value in the list, “Timmy”, has an index of 1.
The third value in the list, “Kenny”, has an index of 2.
The fourth value in the list, “Lenny”, has an index of 3.
To access an item in a list by index number, first, write the name of the list, then write the integer index of the item in square brackets.
For example, to access the element at index 2:
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
print(names[2])
#output
#Kenny
Lists in Python are mutable
When an object is mutable in Python, it means that the value can be changed once the object is created.
Lists are volatile objects, so they can be updated and changed after creation.
Lists are also dynamic. That is, the list can grow and shrink during the lifetime of the program.
Items can be removed from existing lists and new items can be added to existing lists.
It has built-in methods for adding and removing items from the list. For example, to append an element there are methods .append(), .insert(), and .extend().
There are methods .remove(), .pop() and .pop(index) for removing elements.
What does the .append()method do?
The .append() technique provides extra detail to the quit of an already current list.
The widespread syntax appears something like this:
list_name.append(item)
Here’s how it works:
- list-name is the name you gave the list.
- .append() is a list method for appending elements to the end of list_name.
- item is the specified single item to add.
Using .append() modifies the original list. No new list is created. If you want to add another name to the previously created list, do the following:
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
#add the name Dylan to the end of the list
names.append("Dylan")
print(names)
#output
#['Jimmy', 'Timmy', 'Kenny', 'Lenny', 'Dylan']
What’s the difference between the .append() and .insert() methods?
The difference between the two methods is that .append() adds an item to the end of the list, while.insert() inserts an item at a specific position in the list.
As we saw in the previous section, .append() always appends the items passed as arguments to the function at the end of the list.
If you don’t want the item to be added to the end of the list, you can use it. insert() to specify where to add the item.
The general syntax looks like this:
list_name.insert(position,item)
Here’s how it works:
- list-name is the name of the list.
- . insert() is a list method for inserting elements into a list.
- The position is the first argument of the method. This is always an integer. Specifically, the index number where the new element will be placed.
item is her second argument against the method. Here you specify a new item to add to the list.
Suppose we have the following list of programming languages:
programming_languages = ["JavaScript", "Java", "C++"]
print(programming_languages)
#output
#['JavaScript', 'Java', 'C++']
If you want to insert “Python” as a new item at the beginning of the list, use the .insert() method and specify a position of 0. (Note that the first value in the list is always index 0.)
programming_languages = ["JavaScript", "Java", "C++"]
programming_languages.insert(0, "Python")
print(programming_languages)
#output
#['Python', 'JavaScript', 'Java', 'C++']
If instead, you want “JavaScript” to be the first item in the list and “Python” to be added as a new item, specify a position of 1.
programming_languages = ["JavaScript", "Java", "C++"]
programming_languages.insert(1,"Python")
print(programming_languages)
#output
#['JavaScript', 'Python', 'Java', 'C++']
The .insert() method is a bit more flexible compared to the .append() method which just appends the new item to the end of the list.
What’s the difference between the .append() and.extend() methods?
What if you want to add multiple items to the list at once instead of adding them individually?
You can add multiple items to the end of the list using the .append() method.
Suppose you have a list containing only two programming languages.
programming_languages = ["JavaScript", "Java"]
print(programming_languages)
#output
#['JavaScript', 'Java']
After that, we will add two more languages.
In this case, we pass a list of two new values to append to .append() as an argument.
programming_languages = ["JavaScript", "Java"]
#add two new items to the end of the list
programming_languages.append(["Python","C++"])
print(programming_languages)
#output
#['JavaScript', 'Java', ['Python', 'C++']]
If you look closely at the output above [`JavaScript’, ‘Java’, [‘Python’, ‘C++’]] you can see that the new list is appended to the end of the existing list. list.
So .append() appends a list within a list.
Lists are objects, and when you add another list to a list using .append(), the new item is added as a single object (item).
Suppose you already have two lists, like this:
names = ["Jimmy", "Timmy"]
more_names = ["Kenny", "Lenny"]
What if you wanted to combine the contents of both lists into one by appending the contents of more_names to the names?
Using the .append() method for this purpose creates another list in the name.
names = ["Jimmy", "Timmy"]
more_names = ["Kenny", "Lenny"]
#add contents of more_names to names
names.append(more_names)
print(names)
#output
#['Jimmy', 'Timmy', ['Kenny', 'Lenny']]
What if you wanted to combine the contents of both lists into one by appending the contents of more_names to the names?
So .append() adds the new item as another list by appending the object to the end.
To actually concatenate (append) lists, joining all the elements of one list to another, you need to use the .extend() method.
The general syntax looks like this:
Using the .append() method for this purpose creates another list in the name.
list_name.extend(iterable/other_list_name)
- list_name is the name of one of the lists.
- .extend() is a method that appends all the contents of one list to another list.
- The iterable can be any iterable, like another list, such as another_list_name. In this case, other_list_name is a list concatenated with list_name and its contents are appended individually to the end of list_name as separate items.
So if you replace .append() with .extend() in the previous example, the output would be:
names = ["Jimmy", "Timmy"]
more_names = ["Kenny", "Lenny"]
names.extend(more_names)
print(names)
#output
#['Jimmy', 'Timmy', 'Kenny', 'Lenny']
Using .extend() extended the list of names, increasing its length by 2.
The way .extend() works is that it takes a list (or other iterable) as an argument, iterates through each element, then appends each element in the iterable to the list.
There is another difference between .append() and .extend().
As we saw earlier, when we want to append a string, .append() appends an entire element to the end of the list.
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
#add the name Dylan to the end of the list
names.append("Dylan")
print(names)
#output
#['Jimmy', 'Timmy', 'Kenny', 'Lenny', 'Dylan']
If you use .extend() to append the string to the end of the list instead, each character in the string is added to the list as a separate element.
This is because strings are iterable and .extend() iterates over the iterable argument passed to it.
So the above example becomes:
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
#pass a string(iterable) to .extend()
names.extend("Dylan")
print(names)
#output
#['Jimmy', 'Timmy', 'Kenny', 'Lenny', 'D', 'y', 'l', 'a', 'n']
Conclusion
In summary, the .append() method is used to add items to the end of an existing list without creating a new list.
When used to append a list to another list, it creates a list within the list.