howtouselinux

4 Ways to Append List in Python

Table of Contents

Python is a versatile language that you can use for all sorts of purposes. One of the things that makes it so great is its ability to handle lists. Lists are one of the most basic data structures in Python, and they allow you to store multiple items in a single variable. In this blog post, we will discuss four different ways to append a list in Python.

Append list in Python

The best way to append list in Python is to use append method.  It will add a single item to the end of the existing list.  The Python append() method only modifies the original list. It doesn’t return any value. The size of the list will increase by one.

  • 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.
  • list.append() method is used for adding an item to the end of an existing list, without creating a new list.
  • When it is used for adding a list to another list, it creates a list within a list.
  • the element that you’re adding will be added to the end of the list. So, if you want to add an element to the beginning of a list, you’ll need to use a different method.

 

Syntax of Python list append method

  • 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.

 

Append list with number in Python

To append a number to a list in Python, you will need to use the append() method. This method takes a single parameter, which is the number that you want to add to the list.In the code below we will check out 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’]

Append list with string in Python

The append() method  can add string to the end of the list .

Here is an example:
characters = [‘Tokyo’, ‘Lisbon’, ‘Moscow’, ‘Berlin’]
characters.append(‘Nairobi’)
print(‘Updated list:’, characters)
Updated list: [‘Tokyo’, ‘Lisbon’, ‘Moscow’, ‘Berlin’, ‘Nairobi’]

Append list with a Single Item in Python

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

>> x = [1, 2, 3, 4]
>>> y = (5, 6)
>>> x.append(y)
>>> x
[1, 2, 3, 4, (5, 6)]

Append new list to the list in Python

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

>>>#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

We can see from the below output that a new list is appended at the end of our old list.
Output: [‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’,[1,2,3,4,5]]

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

Append list in 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]

 

FAQ about append list in Python

How does the append() method work?

The append() method works by adding the element to the end of the list. If the list is empty, then the append() method will create a new list and add the element to it.

understanding list index in Python

The list in Python is indexed. The elements in a list are indexed according to a definite sequence. The indexing of a list is starting 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. We can create the same indexed item as another one with a different index and the list will still accept it.

Difference between Python List append and Python List insert

  • Python list .append([item]) appends Python elements to a Python lists whereas Python list .insert([index],[item]) inserts Python elements to Python lists. Python list insert method takes an additional argument for the Python index to place Python element in Python list.
  • Python list .insert() can be used when Python index is known, whereas Python list .append() could be used to append Python elements to end of a Python list.

 

Difference between Python List append and Python List extend

When calling append method data will be appended as a single unit.
list1 = [1, 2, 3]
list2 = [3, 4, 5]

list1.append(list2)
# length of list1 will be 4 now.
print(list1) # should return [1, 2, 3, [3, 4, 5]]
print(len(list1)) # should return 4

# when calling extend method on list1, it appends elements in list 2 to the list 1
list1.extend(list2)
print(list1) #should return [1, 2, 3, 3, 4, 5]
print(len(list1)) #should return 6

# Another Example
list3 = [1, 2, 3]
list4 = [1, 2, 3]
list3.append(‘abc’) # will return [1, 2, 3, ‘abc’]
list4.extend(‘abc’)# will return [1, 2, 3, ‘a’, ‘b’, ‘c’]

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

If you meet any issues with Linux, record them with Loom and send the link to us here. We will reply ASAP.

Load WordPress Sites in as fast as 37ms!