Mutable sequential data types in Python - List Object

Nerd, Solutions 

There are mutable sequential data types supported in Python, and one of those sequential data types is the List, which is the list of objects, identifiers or other data types in Python - comma separated - enclosed in a pair of square brackets.


List object declaration and initialization

List declaration:

List object can be declared, and stored into a variable for future reference. To declare a list object we use the list literal syntax.


my_list = []

The above code will declare a new list object, which is an empty list, and store it into the my_list variable.

my_list = [1,2,3]

This above code will declare a new list object, which contains elements 1, 2 and 3; And the list object will be stored in the my_list variable.

List object initialization:

List object can be initialized, when it declared. There are four common ways a list object can be initialized, and those ways are:

Initialized to an empty list object:

This initialization is very simple all it takes is a pair of square brackets with nothing inside.


my_list = []

my_list variable is going to be initialized to an empty list.

Initialized to a list of elements:


This initialization is slightly similar to the above initialization. The only difference is in this one a list of objects or identifiers are enclosed by a pair of square brackets.


my_list = [1,2,3, True, None]

my_list variable is going to be initialized with the list of elements that are enclosed inside square brackets.

 Initialized from a copy of another list object:

In this initialization we copy elements of one list object and assign them into a newer list. 


first_list = [1,2,3,4]

second_list = first_list[1:3]

The second_list will be initialized into a newer list object that contains elements 2 and 3 that are copied from the first_list variable.

For this initialization you can use the above method, which is a getslice syntax, or you can use the copy method from python standard library. Both methods will achieve the same goal.

 Initialized to a constant value with a specified number of elements:

This initialization use a special syntax, which is different from all other initialization methods. To initialize a list with 10 elements of character 'a' we use this syntax.


my_list = ['a']*10

my_list variable will be initialized to a list of 10 elements, which are character a.

You can choose any one of these four methods to initialize your list. You may prefer one over the other depending on your list needs.

Conversion of List object to other data types

List object can be converted into other Python data types except to a numeric data types (integer and floating point numbers). You can convert a list to a Boolean value, a string and a tuple (which is an object like a list, but not mutable).

Every conversion requires a function that is designed to do the conversion job. These functions are str(), bool(), and tuple().

Convert list object into string:

This conversion is done by using the str() function, which takes one argument and returns a string representation of it.


my_list = [1,2,3,4]

my_list_string = str(my_list)

The string equivalent of my_list will be stored in the my_list_string variable.

 Convert list object into a Boolean value:


The list object can be converted into a Boolean value either True or False. To do this we need to use the bool() function, which takes one argument and return either True or False depending on the passed argument. In list object conversion this function will return false only, if the list object is empty.

Conversion of list object to tuple:

Tuple is another data type in Python, which is similar to a list, but it's not mutable like a list (you can't change the value of its elements once created). The conversion from a list object into a tuple object is done by using the tuple() function, which takes one argument, and return a tuple equivalent of the list object. 

On the opposition direction, string and tuple objects can be converted into the list objects. However, Boolean or number objects cannot be converted into the list objects. This conversion is done using the list function, which also takes one argument.

List mutability

Because a list is a mutable object, which means its elements can be changed or removed from the list. There are two ways to make changes to a list object. The first is setitem and setslice, which directly replace an item or a slice in a list object, respectively, and delitem and delslice, which delete an item or a slice from a list object.


This is used to change the value of an already existed element. The syntax is the name of the variable that holds the list followed by the square brackets encloses an index, which represent the position of the element we want change.


my_list = [1,2,34]

my_list[0] = 20

The above code is going to change the value of an element in index 0 of my_list list from 1 to 20.


This can be used to add new elements into the list, or can be used to change value of more than one element in the list. 


my_list = [1,2,3,4,5]

my_list[0:2] = [20,10]

This code will change the value of elements in index 0 and 1 from 1 and 2 to 20 and 10 respectively.

Also setslice can be used to add new elements in the list. This is done by specifying the index that is not already occupied in the list.


my_list = [1,2]

my_list[2:] = [3,4,5]

This code will add 3,4, and 5 into my_list list, so my_list is going to have 5 elements instead of 2.

Another thing that can be done by using the setslice is, we can replace a slice with a longer slice. Python will create another slice with the additional element. We can also replace a slice with a shorter slice. Python will delete all the remain slice that are outer of the range.

We can insert the slice by specifying the slice location, by using the same number in both slice location.


my_list = [1,2,3,4]

my_list[1:1] = [1]

This will insert the element one into position one of the list.

Delete Item from the list:


The mutability of the list does not end with setitem and setslice. We can also delete elements from the list. This is done using the del keyword.


my_list = [1,2,3,4]

del my_list[0]

This code will delete an element at index 0 of the my_list list.

The same method can be used to delete the slice of the list. 


my_list = [1,2,3,4]

del my_list[0:2]

This code will delete the elements from index 0 to index 1, which are 1 and 2 from my_list.

List mutability through methods and attributes

Another way to modify list element is by using built-ins method of the list object.

To add a new element into the list, we can use the append() method, which takes only one argument that is value we want to add into the list. This method will insert our element at the back of the list. The function is the procedure, which means a None value is returned, when invoked.


my_list = [1,2,3]


This code will add number 4 into my_list.

Another method that can be used to modify the list object is the insert() method, which takes two arguments. The first argument is the number that specify the index of the element we want to change, and the second argument is the value that we want to change to.


my_list = [1,2,3,4]


This code will change the element at index 0, which is 1 to 100.

Also this method is the procedure, means no value is returned from this method.

The method extend() is used extends a list by concatenating it with another list, which take a list argument, and concatenates it with the list object.

Removing item via a method:

The method that deletes an item from a list is remove, which takes one argument and deletes the first occurence of the argument from the list. If the input argument is not in the list, a value error is reported. The passed argument is not the position (index), but the value that need to be deleted from the list.


my_list = [1,2,3,4]


This code will remove element 2 from the list.

Another methods can be applied into the list object. These includes reverse() - reverse the order of the elements in the list, sort() - sort the order of the elements in the list. Both these methods are procedures.

Three important things to note about list object