Skip to Content

3 Examples of List Append Method in Python3

List in Python are indexed and have a definite count while initializing.

The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index and the last item index is n-1 where n is the number of items in a list.

Each element in the list has its indexed place in the list, which allows duplicating of elements in the list i.e we can create the same indexed item as another one with a different index and the list will still accept it.

In this article, we will cover 3 examples to append list in Python

Python List Append method

The append() method in Python adds a single item to the end of the existing list.

With .append(), we can add a number, list, tuple, dictionary, user-defined object, or any other object to an existing list. However, we need to keep in mind that .append() adds only a single item or object at a time:

After appending to the list, the size of the list increases by one.

  • SYNTAX  – list_name.append(item)
  • PARAMETERS – The append() method takes a single item as an input parameter and adds that to the end of the list.
  • Return Value – The append() method only modifies the original list. It doesn’t return any value as a return but will just modify the created list.

Add Number to a List with append method

In the code below we will look at how to add a new numeric item to a list.
Code:

>>># list of strings
>>>string_list = [‘Medium’,’Python’,’Machine Learning’,’Data Science’]
>>>#adding a new int item to the string_list
>>>string_list.append(1)
>>>#printing appended list
>>>print(string_list)

Output:
[‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’]

Add new list to a List with append method

Apart from adding a string and a numeric data type we can also add separate list to a list as below
Code:

>>>#lets create a new list
>>>new_list = [1,2,3,4,5]
>>>#append this list to our string_list
>>>string_list.append(new_list)
>>># print the appended list
>>>string_list

Output:
[‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’,[1,2,3,4,5]]

We can see from the above output that a new list is appended at the end of our old list.

We will get the whole list as an indexed item using the below code.
Code:
string_list[5]
Output:
[1,2,3,4,5]

Using append() in Python for loop

Another common use case of .append() is to completely populate an empty list using a for loop.

Inside the loop, we can manipulate the data and use .append() to add successive results to the list.

In the example below, we create an empty list and assign it to the variable num. Then, using a for loop, we add a sequence of elements (integers) to the list that was initially empty:

>> num = []
>>> for i in range(3, 15, 2):
num.append(i)

We check the value of the variable to see if the items were appended successfully and confirm that the list is not empty anymore:

>>> num
[3, 5, 7, 9, 11, 13]
Tip: We commonly use append() to add the first element to an empty list.

reference:

3 Examples to append list in Python