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.

Back to: Python Programming Course > Python Lists

Leave a Reply

Your email address will not be published.

IPCisco is the Winner of 2019 “Best Certification Study Journey” Category! We are also Finalist of 2020 & 2021 in Cisco IT Blog Awards!

Cisco-ITBlogAwards-2019-Winner-IPCisco-k
ITBlogAwards_2020_Badge-Finalist-BestCertStudy-ipcisco
ITBlogAwars-2021-ipcisco

Python Programming Course

Collapse
Expand
IPCisco on Social Media!

Latest Blog Posts

top-ten-network-certifications-ipcisco
metaverse-jobs-ipcisco.com
Wifi-6-vs-wifi-5-ipcisco.com
16-Reasons-To-Learn-Linux
gns3-vm-ware-installation-guide