Python List Add: Append Method

python-list-methods-append-method-ipcisco

In this lesson we will focus on python Append() method and we will see how to do Python list add. In other words we will learn how to put a new member at the end of the list. Here, we will use append method to do this.

 

Let’s do an example for python list add function and learn this method better.

 

elves = ["Legolas", "Elrond", "Galadriel"]
elves.append("Arwen")
print(elves)

 

With this code, a new string member will be in our string list.

 

['Legolas', 'Elrond', 'Galadriel', 'Arwen']

 

python-list-methods-append-method-ipcisco

 

Let’s do the similar example with a number list.

 

numbers =[10,18,23,45,82]
numbers.append(100)
print(numbers)

 

For this code, the output of this number list will be like below:

 

[10, 18, 23, 45, 82, 100]

 

We can also add one string list into other string list with this append method. In the below example, we will ad done string list in another string list.

 

LoR = ["Legolas", "Elrond", "Galadriel"]
human = ["Aragorn","Theoden"]
LoR.append(human)
print(LoR)

 

The output of this python code will be like below:

 

['Legolas', 'Elrond', 'Galadriel', ['Aragorn', 'Theoden']]

 

Now  let’s do the same python list append example with numbers.

 

numbers1 = [1,2,3,4,5,6]
numbers2 = [10,11,12]
numbers1.append(numbers2)
print(numbers1)

 

The output of this list append example will be:

 

[1, 2, 3, 4, 5, 6, [10, 11, 12]]

 

As you can see above, the second list (numbers2) is showed as a new member of the first list. And the second list is added at the end of the first list (numbers1). This is different than combining two different strings. Because, here, the second list (numbers2) is only a different member of the first list (numbers1). The members of the second list (numbers2) is not added to the first list (numbers1)one by one.

 

In this python list add lesson, we have learned how to put a member at the end of a list. We are using python append function to add a member at the end of a list. You can do different python list examples to learn this method better. It is a very popular and one of the most used python methods. Good luck!

 

Back to: Python Programming Course > Python Lists

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k

Python Programming Course