Python List Append

python-append-method-ipcisco-1

Python List Append is one of the most used python functions. 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 fucntion 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 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. Required fields are marked *

Contact

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

Cisco-ITBlogAwards-2019-Winner-IPCisco-k