Python List Append

python-append-method-ipcisco-1

Python List Append is one of the most used Python List Methods. With this Python List Append method, we can add a new member to the end of the lists. Let’s give some example to learn this python function better.

 

Below, we will use append() method to add new string members at the end of the string.

 

characters = ["Frodo", "Aragorn", "Arwen", "Gandalf"]
characters.append("Legolas")
characters.append("Gimli")
characters.append("Boromir")
print(characters)

 

The output of this Python List Append code will be like below:

 

['Frodo', 'Aragorn', 'Arwen', 'Gandalf', 'Legolas', 'Gimli', 'Boromir']

 


You can learn also How to Sort Python Lists


 

You can also watch this lesson on Youtube!

We can do the same thing that we do above, with plus (+) operator like blow. Here, we will use two different string lists and we will combine these string lists in one string.

 

characters1 = ["Aragorn", "Arwen", "Gandalf"]
characters2 = ["Legolas", "Gimli", "Boromir"]
print(characters1+characters2)

The output of this python code will be like below:

['Aragorn', 'Arwen', 'Gandalf', 'Legolas', 'Gimli', 'Boromir']

 

python-list-append-ipcisco-1

 

We can use Python List Append code for number lists also. Below, we will add 100 to the end of number list.

 

numbers = [1,2,3,4,5]
numbers.append(100)
print(numbers)

 

The output of this python command will be like below:

 

[1, 2, 3, 4, 5, 100]

 

python-list-append-ipcisco-2

If we use Python List Append code with two lists, the second list is added to the previous list like below:

 

characters1 = ["Aragorn", "Arwen", "Gandalf"]
characters2 = ["Legolas", "Gimli", "Boromir"]
 characters1.append(characters2)
 print(characters1)

 

['Aragorn', 'Arwen', 'Gandalf', ['Legolas', 'Gimli', 'Boromir']]

 

python-append-method-ipcisco-1

 

If you would like to extend the list, then you should use another function of python. Python list extend function an do this job. Let’s give an example for Python list extend command. Here, we will use extend () function with the new added list as parameter.

 

characters1 = ["Aragorn", "Arwen", "Gandalf"]
characters2 = ["Legolas", "Gimli", "Boromir"]
characters1.extend(characters2)
print(characters1)

 

The output of this python code will be like below:

 

['Aragorn', 'Arwen', 'Gandalf', 'Legolas', 'Gimli', 'Boromir']

 

 

python-list-extend-method-ipcisco-1

As you can see above, the new list has created as we use plus (+) sign.

 

In this lesson, we have learned how to use Python List Append code. We have done different examples on append function of python. You can increase your experience on this python method with more examples. You can also use this method with your different python codes.

gokhan-kosem-instructor-ipcisco

Gokhan Kosem is a Network Engineer, Instructor and the Founder of IPCisco.com with 15+ years of experience in Cisco, Nokia, Huawei, Juniper, Linux, Service Provider Networks, Routing and Switching technologies.

He has worked on the backbone networks of major service providers and network vendors including Nortel, Alcatel-Lucent (Nokia) and has extensive hands-on experience with Cisco, Huawei, Juniper and Nokia networking technologies.

He has trained thousands of networking students worldwide through IPCisco.com, Udemy, books, labs, quizzes, and educational content across multiple social media platforms.

IPCisco.com | Best Route to Your Dreams

Back to: Python Programming Course > Python Lists

Leave a Reply

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

ipcisco gold membership

Python Programming Course

Collapse
Expand