List append Python

How do you append (or add) new values to an already created list in Python? I will show you how in this article.

But first things first...

What is a List in Python?

A List is a data type that allows you to store multiple values of either the same or different types in one variable.

Take a look at the example below:

age = 50
name = "Python"
isRunning = False

In this code, age,

item1 = "banana"
item2 = "apple"
item3 = "orange"
0 and
item1 = "banana"
item2 = "apple"
item3 = "orange"
1 only hold one value each, of the
item1 = "banana"
item2 = "apple"
item3 = "orange"
2,
item1 = "banana"
item2 = "apple"
item3 = "orange"
3, and
item1 = "banana"
item2 = "apple"
item3 = "orange"
4 data types, respectively.

Let's say you wanted to store all the things you bought in the market using this approach:

item1 = "banana"
item2 = "apple"
item3 = "orange"

Creating three separate variables for related items may not be the best approach.

With lists, you can create a variable that holds multiple values. Here's how:

numbers = [1, 2, 3]

strings = ["list", "dillion", "python"]

mixed = [10, "python", False, [40, "yellow"]]

The

item1 = "banana"
item2 = "apple"
item3 = "orange"
5 variable is a list containing three number values.

The

item1 = "banana"
item2 = "apple"
item3 = "orange"
6 variable is a list containing three string values.

The

item1 = "banana"
item2 = "apple"
item3 = "orange"
7 variable is a list containing a number, a string, a boolean, and even another list.

So for the items you bought at the market, you can store them like this:

items = ["banana", "apple", "orange"]

And you can access each item using its index position in the list, starting from 0 (as lists are zero-indexed in Python):

print(items[0], items[1], items[2])
# banana apple orange

How to Append Data to a List in Python

We've briefly seen what lists are. So how do you update a list with new values? Using the

item1 = "banana"
item2 = "apple"
item3 = "orange"
8 method.

The

item1 = "banana"
item2 = "apple"
item3 = "orange"
9 method receives one argument, which is the value you want to append to the end of the list.

Here's how to use this method:

mixed = [10, "python", False]

mixed.append(40)

print(mixed)
# [10, 'python', False, 40]

Using the

item1 = "banana"
item2 = "apple"
item3 = "orange"
9 method, you have added
numbers = [1, 2, 3]

strings = ["list", "dillion", "python"]

mixed = [10, "python", False, [40, "yellow"]]
1 to the end of the
item1 = "banana"
item2 = "apple"
item3 = "orange"
7 list.

You can add any data type you want, including other lists:

mixed = [10, "python", False]

mixed.append([True, "hello"])

print(mixed)

# [10, 'python', False, [True, 'hello']]

Wrapping up

Lists are useful for creating variables that hold multiple values (especially when these values are related)

Lists have many methods in Python that you can use to modify, extend, or reduce the lists. In this article, we've looked at the

item1 = "banana"
item2 = "apple"
item3 = "orange"
9 method which adds data to the end of the list.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

In this article, you'll learn about the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 method in Python. You'll also see how
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 differs from other methods used to add elements to lists.

Let's get started!

What are lists in Python? A definition for beginners

An array in programming is an ordered collection of items, and all items need to be of the same data type.

However, unlike other programming languages, arrays aren't a built-in data structure in Python. Instead of traditional arrays, Python uses lists.

Lists are essentially dynamic arrays and are one of the most common and powerful data structures in Python.

You can think of them as ordered containers. They store and organize similar kind of related data together.

The elements stored in a list can be of any data type.

There can be lists of integers (whole numbers), lists of floats (floating point numbers), lists of strings (text), and lists of any other built-in Python data type.

Although it is possible for lists to hold items of only the same data type, they are more flexible than traditional arrays. This means that there can be a variety of different data types inside the same list.

Lists have 0 or more items, meaning there can also be empty lists. Inside a list there can also be duplicate values.

Values are separated by a comma and enclosed in square brackets,

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
9.

How to create lists in Python

To create a new list, first give the list a name. Then add the assignment operator(

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
0) and a pair of opening and closing square brackets. Inside the brackets add the values you want the list to contain.

#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

Lists maintain an order for each item.

Each item in the collection has an its own index number, which you can use to access the item itself.

Indexes in Python (and every other modern programming language) start at 0 and increase for every item in the list.

For example, the list created earlier on had 4 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 element in the list by its index number, first write the name of the list, then in square brackets write the integer of the element's index.

For example, if you wanted to access the element that has an index of 2, you'd do:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny

Lists in Python are mutable

In Python, when objects are mutable, it means that their values can be changed once they've been created.

Lists are mutable objects, so you can update and change them after they have been created.

Lists are also dynamic, meaning they can grow and shrink throughout the life of a program.

Items can be removed from an existing list, and new items can be added to an existing list.

There are built-in methods for both adding and removing items from lists.

For example, to add items, there are the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7,
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
2 and
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3 methods.

To remove items, there are the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
4,
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
5 and
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
6 methods.

What does the names = ["Jimmy", "Timmy", "Kenny", "Lenny"] 7 method do?

The

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 method adds an additional element to the end of an already existing list.

The general syntax looks something like this:

list_name.append(item)

Let's break it down:

  • names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    9 is the name you've given the list.
  • names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    7 is the list method for adding an item to the end of
    names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    9.
  • list_name.append(item)
    
    2 is the specified individual item you want to add.

When using

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7, the original list gets modified. No new list gets created.

If you wanted to add an extra name to the list created from earlier on, you would 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 names = ["Jimmy", "Timmy", "Kenny", "Lenny"] 7 and names = ["Jimmy", "Timmy", "Kenny", "Lenny"] print(names[2]) #output #Kenny 2 methods?

The difference between the two methods is that

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 adds an item to the end of a list, whereas
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
2 inserts and item in a specified position in the list.

As you saw in the previous section,

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 will add the item you pass as the argument to the function always to the end of the list.

If you don't want to just add items to the end of a list, you can specify the position you want to add them with

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
2.

The general syntax looks like this:

list_name.insert(position,item)

Let's break it down:

  • names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    9 is the name of the list.
  • names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    2 is the list method for inserting an item in a 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']
    
    2 is the first argument to the method. It's always an integer - specifically it's the index number of the position where you want the new item to be placed.
  • list_name.append(item)
    
    2 is the second argument to the method. Here you specify the new item you want to add to the list.

For example, say you had the following list of programming languages:

programming_languages = ["JavaScript", "Java", "C++"]

print(programming_languages)

#output
#['JavaScript', 'Java', 'C++']

If you wanted to insert "Python" at the start of the list, as a new item to the list, you would use the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
2 method and specify the position as
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']
5. (Remember that the first value in a list always has an index of 0.)

programming_languages = ["JavaScript", "Java", "C++"]

programming_languages.insert(0, "Python")

print(programming_languages)

#output
#['Python', 'JavaScript', 'Java', 'C++']

If you instead had wanted "JavaScript" to be the first item in the list, and then add "Python" as the new item, you would specify the position as

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']
6:

programming_languages = ["JavaScript", "Java", "C++"]

programming_languages.insert(1,"Python")

print(programming_languages)

#output
#['JavaScript', 'Python', 'Java', 'C++']

The

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
2 method gives you a bit more flexibility compared to the
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 method which only adds a new item to the end of the list.

What's the difference between the names = ["Jimmy", "Timmy", "Kenny", "Lenny"] 7 and names = ["Jimmy", "Timmy", "Kenny", "Lenny"] print(names[2]) #output #Kenny 3 methods?

What if you want to add more than one item to a list at once, instead of adding them one at a time?

You can use the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 method to add more than one item to the end of a list.

Say you have one list that contains only two programming languages:

programming_languages = ["JavaScript", "Java"]

print(programming_languages)

#output
#['JavaScript', 'Java']

You then want to add two more languages, at the end of it.

In that case, you pass a list containing the two new values you want to add, as an argument to

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
0

If you take a closer look at the output from above,

list_name.insert(position,item)
3, you'll see that a new list got added to the end of the already existing list.

So,

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 adds a list inside of a list.

Lists are objects, and when you use

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 to add another list into a list, the new items will be added as a single object (item).

Say you already had two lists, like so:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
1

What if wanted to combine the contents of both lists into one, by adding the contents of

list_name.insert(position,item)
6 to
list_name.insert(position,item)
7?

When the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 method is used for this purpose, another list gets created inside of
list_name.insert(position,item)
7:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
2

So,

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 adds the new elements as another list, by appending the object to the end.

To actually concatenate (add) lists together, and combine all items from one list to another, you need to use the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3 method.

The general syntax looks like this:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
3

Let's break it down:

  • names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    9 is the name of one of the lists.
  • names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    3 is the method for adding all contents of one list to another.
  • programming_languages = ["JavaScript", "Java", "C++"]
    
    print(programming_languages)
    
    #output
    #['JavaScript', 'Java', 'C++']
    
    4 can be any iterable such as another list, for example,
    programming_languages = ["JavaScript", "Java", "C++"]
    
    print(programming_languages)
    
    #output
    #['JavaScript', 'Java', 'C++']
    
    5. In that case,
    programming_languages = ["JavaScript", "Java", "C++"]
    
    print(programming_languages)
    
    #output
    #['JavaScript', 'Java', 'C++']
    
    5 is a list that will be concatenated with
    names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    9, and its contents will be added one by one to the end of
    names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
    
    print(names[2])
    
    #output
    #Kenny
    
    9, as separate items.

So, taking the example from earlier on, when

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 is replaced with
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3, the output will look like this:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
4

When we used

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3, the
list_name.insert(position,item)
7 list got extended and its length increased by 2.

The way

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3 works is that it takes a list (or other iterable) as an argument, iterates over each element, and then each element in the iterable gets added to the list.

There is another difference between

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 and
names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3.

When you want to add a string, as seen earlier,

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 adds the whole, single item 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 used

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3 instead to add a string to the end of a list ,each character in the string would be added as an individual item to the list.

This is because strings are an iterable, and

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

print(names[2])

#output
#Kenny
3 iterates over the iterable argument passed to it.

So, the example from above would look like this:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
6

Conclusion

To sum up, the

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]
7 method is used for adding an item to the end of an existing list, without creating a new list.

When it is used for adding a list to another list, it creates a list within a list.

If you want to learn more about Python, check out freeCodeCamp's Python Certification. You'll start learning in an interacitve and beginner-friendly way. You'll also build five projects at the end to put into practice what you learned.

Thanks for reading and happy coding!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Dionysia Lemonaki

Learning something new everyday and writing about it


If this article was helpful, tweet it.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Is += list the same as append?

For a list, += is more like the extend method than like the append method. With a list to the left of the += operator, another list is needed to the right of the operator. All the items in the list to the right of the operator get added to the end of the list that is referenced to the left of the operator.

Can I use append in list Python?

Lists are sequences that can hold different data types and Python objects, so you can use . append() to add any object to a given list.

What is the use of append () function in list?

Python's append() function inserts a single element into an existing list. The element will be added to the end of the old list rather than being returned to a new list. Adds its argument as a single element to the end of a list. The length of the list increases by one.

How do I append a list to a list?

append() adds a list inside of a list. Lists are objects, and when you use . append() to add another list into a list, the new items will be added as a single object (item).