python list

Python List

The list is a built-in data type. Which store collection of different types of data which can be string, integer, float, date, etc. In this tutorial, we will learn how to create a list, its properties, and the different methods applied to lists.

How to create a list?
In python, a list can be created by placing all the elements inside the square brackets[] and separated by comma (,). The list supports any type of data which means we can create a list with the collection of multiple types of data.


#list with only integer data
tqlisit =[1,2,3]
#list with multiple data types: integer,float,string
tqlist = [1,'thequick',2,'blog',4.5]

list can also have another list inside it. This is called nested list:

#list with multiple list inside it.
quicklist = ['hello',100,[1,2,3],[1,'thequick',2,'blog',4.5],['jan','feb','mar']

Properties of the list:


List elements are accessible through their position which means by their index. The first element of the list is 0 index element and 2nd element is 1 index element and so on.
let’s suppose we have the below list.

tqlist = [1,'thequick',2,'blog',4.5]

Now we can acess it’s element by index value


it prints first element of the list i.e, ‘1‘.

print (tqlist[3])

it returns the 4th element of the list i.e. ‘blog‘.

Negative Indexing in the list.

The list also supports negative indexing as well. We can get the last element by -1 index, second last element by -2, and so on.

tqlist = [1,'thequick',2,'blog',4.5]

it returns the last element of the list, i.e. ‘4.5


it returns the 4th element from the last, i.e. ‘thequick


The list is a mutable data type which means it allows us to change its data on the basis of element index.
For example, we have the below list and we want to change the first element to 100 from 1.we can do it as follows:

tqlist = [1,'thequick',2,'blog',4.5]
tqlist[0] = 100

if we run the above program the output will be as below:

3. Not homogenous

The list is not a homogenous data type which means it supports multiple types of data in a single list element. which means we can create a list with the collection of elements of a single data type and elements of multiple data types as well.

4. Allows duplicates

The list supports duplicate elements value which means we can have the same value in different indexes:
For example, we can have the same value ‘2’ in index 1,2,3,4 and ‘hello’ in index 5 and 7:

tqlisit = [1,2,2,2,2,'hello','blog','hello']