Adding data to a list in Python

Python lists are one of the most common and popular data structure of python. Almost every python programmer uses them for writing great programs. Python lists are almost similar to the vectors in C++ or arrays in Javascript. Lists are mutable, which makes them editable after creation. Lists can contain elements like numbers, strings, dictionaries, tuple, boolean, lists, and any other python object.

Adding data to a list in Python

In this tutorial, we will learn how to add data into a python list. We will focus on the following methods for adding data to a list.

  • append() append a single element at the end of the list.
  • extend() extends the list by appending items from an iterable.
  • insert() – insert a given element at a given position on the list.

To follow this tutorial, you must need to have python installed in your system, if you don’t have python installed in your system then you can follow our step by step guide on installing python in Linux.

Using append() function

The append() method is one of the best methods of the python list for adding data into a list. This method is used to append a python object into the list, i.e., it adds an element at the end of the list, which increases the length of the list by 1. The only drawback of this method is that we can only add the data at the end of the list.

Syntax:

The syntax of the append() function is shown below:

list_name.append(obj)

Parameters:

The append() method accepts only one argument obj, which is the object that we want to append to the list. We can give any python object to the argument like strings, numbers, tuple, dictionaries, or another list.

Return Value:

The append() method does not return any value it only append the python object into a list.

Examples:

The below code block shows how to append strings, numbers, and boolean values to a python list. You can copy the below code by clicking the copy button present at the code block’s top menu bar.

# creating a string variable
string_var = "Codeunderscored"
# creating a number
num_var = "101"
# creating a boolean variable
bool_var = True
# creating a list
list_var = [1, 2, 3, 4, 'one', 'two', 'three', 'four']
print(list_var)
# appending string to the list
list_var.append(string_var)
print(list_var)
# appending a number to the list
list_var.append(num_var)
print(list_var)
# appending a boolean value to the list
list_var.append(bool_var)
print(list_var)

We created a string variable, a number variable, and a boolean variable in the above code. We also created a python list. Then we use the append() method of the list to append the string, number, and boolean value to the list.

Output:

list append() method to append string, numbers and booleans
list append() method to append string, numbers and booleans

We can also append python objects to a list using the append() method. In fact, we can also append a list into another list. The below code block shows how to append python objects to a list.

# creating a python dictionary
python_dict = {'name':'codeunderscored', 'address':'internet', 'link':'https://codeunderscored.com'}
# creating a python tuple
python_tuple = (1, 2, 3, 4)
# creating a python list
python_list = [0, 9, 8, 7]
# creating another python list
python_list2 = [1, 2, 3, 4, 'one', 'two', 'three', 'four']
print(python_list2)
# appending dictionary to the list
python_list2.append(python_dict)
print(python_list2)
# appending a tuple to the list
python_list2.append(python_tuple)
print(python_list2)
# appending a list to the list
python_list2.append(python_list)
print(python_list2)

In the above code, we created a dictionary, tuple, and two python lists. Then we append each of the dictionary, tuple, and the list into a list using the append() method of the list.

Output:

list append() method to append python objects
list append() method to append python objects

Using insert() function

The insert() method is used to insert a python object into a specified position in the list, i.e., it can be used to add an element at any position by providing the index of that position.

Syntax:

The syntax of the insert() method is shown below:

list_name.insert(index, element)

Parameters:

The insert() method accepts two arguments. The first argument is the index, which is the index number of the position where we want to insert the python object. The second argument is the object that we want to insert into the list. We can provide any python object(lists, tuples, dictionaries, numbers, strings, etc.) as an argument to the insert function.

Return Value:

The insert() method does not return any value it only insert the python object into a list at the given position.

Examples:

The below code block shows a simple demo of the insert() function by inserting some python variables into some specific position in a list.

# creating a string variable
string_var = "Codeunderscored"
# creating a number
num_var = "101"
# creating a boolean
bool_var = True
# creating a list
list_var = [1, 2, 3, 4, 'one', 'two', 'three', 'four']
print(list_var)
# inserting a string at the last of the list
# using the len() function to calculate the length
# of the list
list_var.insert(len(list_var) ,string_var)
print(list_var)
# inserting a number at the third position in the list
list_var.insert(2, num_var)
print(list_var)
# inserting a boolean value at the beginning of the list
list_var.insert(0 ,bool_var)
print(list_var)

In the above code, we created three variables with string, number, and boolean data type. Then we created a python list and used its insert() method to insert the variables’ value into a specified position in the list.

Output:

list insert() method to add numbers, strings and booleans
list insert() method to add numbers, strings and booleans

We can also insert python objects like dictionaries, tuple, and a list using the insert() method of the list. The below code block shows a practical illustration.

# creating a python dictionary
python_dict = {'name':'codeunderscored', 'address':'internet', 'link':'https://codeunderscored.com'}
# creating a python tuple
python_tuple = (1, 2, 3, 4)
# creating a python list
python_list = [0, 9, 8, 7]
# creating another python list
python_list2 = [1, 2, 3, 4, 'one', 'two', 'three', 'four']
print(python_list2)
# inserting a dictionary at a specific position in the list
python_list2.insert(len(python_list2), python_dict)
print(python_list2)
# inserting a tuple at a specific position in a list
python_list2.insert(1, python_tuple)
print(python_list2)
# inserting a list at a specific position in a list
python_list2.insert(0, python_list)
print(python_list2)

In the above code, we created a python dictionary, tuple, and a list. Then we created another list and used its insert() method to insert a dictionary, tuple, and list.

Output:

list insert() method to insert python objects
list insert() method to insert python objects

Using extend() function

The extend() method is used to append all the elements of an iterable(list, tuple, strings) into the list, i.e., it adds all the elements of an iterable at the end of a list. After using the extend() function, the length of the list increase by the length of the iterable.

Syntax:

The syntax of the extend() method is shown below:

list1.extend(iterable_item)

Parameters:

The extend() method accepts only one argument, iterable_item, which is the iterable object whose elements will be appended to the list. We can give any iterable Python object to the argument like strings, tuple, lists, etc.

Return Value:

The extend() method does not return any value it only append the elements of the iterable to the list.

Examples:

We can use the extend() method of a list to extend it with iterable elements like a string, tuple, or another list. The below code block shows how we can extend a list by using a string.

# creating a string variable
string_var = "Codeunderscored"
python_list2 = []
print(python_list2)
# extending the python list with a string
python_list2.extend(string_var)
print(python_list2)

In the above code, we created a string with the text “codeunderscored” and an empty list. Then we use the extend method of the list to extend the list with the elements of the string.

Output:

list extend() method to extend a list with a string
list extend() method to extend a list with a string

In the code above, we have seen how we can use extend a string to a list, but we can also extend other iterables like lists and tuples into a list. The following code shows a practical illustration.

# creating a python tuple
python_tuple = (4, 5, 6)
# creating a python list
python_list = [7, 8, 9]
# creating another python list
python_list2 = [0, 1, 2, 3]
print(python_list2)
# extending the python list with a tuple
python_list2.extend(python_tuple)
print(python_list2)
# extending the python list with another list
python_list2.extend(python_list)
print(python_list2)

In the above code, we first created a tuple and two lists. Then we use the extend() method of one list to extend the data of the tuple and another list into that list.

Output:

list extend() method to extend a list with a tuple and a list
list extend() method to extend a list with a tuple and a list

Conclusion

In today’s tutorial, we learned the various ways in which we can add data to a python list. Learning the ways are very helpful as lists are one of the most important and useful python data structures. You may also refer to our tutorial on formatting terminal with rich text in python, which will show you how to beautifully format the console using python’s powerful, rich library.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *