Python Append

Python Append will add something to the end of your list. It can be anything, a number, object, dictionary, or even another list.

Append simply means “add this thing to the end of that thing.”

list.append(item)

For example if you had a list of [1,2,3] and you wanted to append “4” you would call list.append(4)

This one is also extremely common throughout the Python language and it’s libraries. In fact, it’s extremely popular as a part of fundamental coding languages in general.

Now you’ll know what it means when you see it in Pandas and Numpy.

Let’s take a look at a python append code sample


In [16]:
# Define a list with a few items in it
my_list = [1,2,3,4]
In [17]:
# Then add an item to the end of your list via append
my_list.append(5)

# View your output
my_list
Out[17]:
[1, 2, 3, 4, 5]
In [18]:
# Did you know that you could add a bunch of different data types to a list
my_dict = {"my_key" : "my_value"}
my_list2 = [10,11,12]
my_string = "My String!"
my_float = 4.5643
In [19]:
my_list.append(my_dict)
my_list
Out[19]:
[1, 2, 3, 4, 5, {'my_key': 'my_value'}]
In [20]:
# Or you can add a list of items to a list
my_list.append(my_list2)
my_list.append(my_string)
my_list.append(my_float)
my_list
Out[20]:
[1, 2, 3, 4, 5, {'my_key': 'my_value'}, [10, 11, 12], 'My String!', 4.5643]

Link to code above

Check out more Python Vocabulary on our Glossary Page