How to use Python List Append – with example

  • Reading time:15 mins read

What is a Python List append method?

Python List Append is a method that adds an element to the end of a list. 

 

How to use Python List append?

So in its simplest form, we need to have a list of some descriptions that we can then use to demonstrate the append method. In this example, we are going to use a list of animals = [‘elephant’, ‘tiger’, ‘lion’, ‘giraffe’]. In order to append something to a list, we first need to call the actual list that we are appending to.

 

Learn how to use Python ListAppend online

 

In this case, it’s animals. And then we will now use the keyword append.

This will just let us append whatever we have in these brackets to the end of the list of animals. For example, let’s add in another animal by the name ‘shark’. If we now proceed to print out the “animals list again. If we run this code the name ‘shark’ is added to the end of the list.

 

Appending a list to another list

Using the append method one can also add a list to the end of a preexisting list so that we can have a list of lists or anything like that.

 

Python ListAppend is a useful tool while coding

 

We will still be appending to the list of animals but instead of appending just another text string, we’re going to add another list here. So we’re going to say animals.append([‘fish’, ‘bird’]). Now when we run that we will obtain the list [‘fish’, ‘bird’] inside the main list.

 

Appending a tuple to a list – Python List Append

A tuple is used to store multiple items under one variable. Tuples are usually written with parentheses or round brackets. In this case, we are going to append a tuple containing (‘fish’, ‘bird’). We can now run the statement animals.append((‘fish’, ‘bird’)). So when we run this code we will have the tuple appended to the end of the list of animals.

 

Appending a dictionary to a list – Python List Append

Just like tuples and lists, a dictionary can also be appended to a list. We can demonstrate that by creating a simple dictionary  {‘name’:’fish’, ‘type’: ‘bird’, ‘’}.

 

Python List Append

 

We can now append this dictionary by writing animals.append({‘name’:’fish’,’type’:’bird’}).

 

Iterating and appending to a list with Python list append

It is quite a common thing in programming to want to loop through a list or any iterable object and then add that to the end of a list. So let’s do for x in range(1, 15). Now, this is just going to give us a  list of numbers and lets us append x each time we iterate through the list of animals list. Now inside the for x loop we will do animals.append(x).

Now if we print this out we will obtain a list with numbers 1 to 14 added to the end of the list.

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.