89DEVs

Python List append()

Python List append()

How to add elements to a list in Python? The append() method is used to add an item at the end of the list. To add the element at a specific position, use the insert() method instead. To add multiple items like a list, tuple or string to a list, use the extend() method.

add string to a list

To add a string to a list, use the append() method as shown below. The string is appended at the end of the list. # add string to list colors = ['red', 'yellow', 'green'] colors.append('blue') print(colors) In this example, the list colors contains three string elements: red, yellow and green. The append() method is used to add the string 'blue' to the list. Finally, the append list is printed and the result is shown below. ['red', 'yellow', 'green', 'blue']

add integer to a list

To add an integer to a list, use the append() method as shown below. # add integer to list numbers = [1, 2, 3] numbers.append(4) print(numbers) In this example, the list numbers contains three integer elements: 1, 2 and 3. The append() method is used to add the integer 4 to the list. Finally, the append list is printed and the result is shown below. [1, 2, 3, 4]

add list to list

It is also possible to add a second list to the original list. Use the append() function as shown below. The items of the new list are appended at the end of the original list. The items of the appended list are inside a list in the list, a so called nested list has been created. # add list to list list1 = [1, 2, 3] list2 = [4, 5, 6] list1.append(list2) print(list1) In this example, two lists are defined. The variable list1 contains the integer values 1, 2 and 3. The variable list2 contains the integer values 4, 5 and 6. The append() method is used to append list2 to list1. This results in a list in a list. The list2 is appended at the end of list1 as shown below. [1, 2, 3, [4, 5, 6]] To receive the result as non-nested list as shown below, use the extend() method. [1, 2, 3, 4, 5, 6]

append multiple items

To append multiple items to a list, use the append() method multiple times or use the extend() method. # append multiple items using append() list1 = [1, 2, 3] list1.append(4) list1.append(5) list1.append(6) print(list1) In this example, the append() method is used three times to append the integer values 4, 5 and 6 to the existing list containing the integer values 1, 2 and 3. The result is a list containing all integer values from 1 to 6. [1, 2, 3, 4, 5, 6] The same can be achieved with calling the extend() method just once. # append multiple items using extend() list1 = [1, 2, 3] list1.extend([4, 5, 6]) print(list1) Here, the integer values 4, 5 and 6 are appended to the list in just one call. This provides the same result and is much shorter and more precise and therefore the preferred way of doing it. [1, 2, 3, 4, 5, 6] The extend() method allows to use the range() function to add a range of numbers to the list, while this is not possible using the append() method. # append range of numbers to a list using extend() list1 = [1, 2, 3] list1.extend(range(4, 11)) print(list1) The variable list1 contains the integer values 1, 2, and 3. Then, the extend() method is used in combination with the range() function to add the numbers from 4 to 10 to the list. This results in a list containing all numbers from 1 to 10. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] More explanations about the extend() functions can be found here.

Syntax of List append()

The syntax of the append() method is: list.append(item)

Arguments of List append()

The append() method accepts exactly one argument. An item of type number, string or another list that will be added at the end of the list.

Return value of List append()

The append() method doesn't return any value, it returns None. The passed item is added to the list.

Related functions of append()

Related functions of the list.append() method are: Sets, Dictionaries and numpy arrays have their own append() methods, which can be used to append items.

                
        

Summary


Click to jump to section