LIST Data Structure in PYTHON

LIST Data Structure in PYTHON

LIST Data Structure in PYTHON are just like the arrays, declared in other languages which is a ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.

A LIST Data Structure in PYTHON is formed(or created) by placing all the items (elements) inside square brackets [ ], separated by commas.

It can have any number of items and they may or may not be of different types (integer, float, string, etc.).

A list is mutable, which suggests we will modify the list


If we want to represent a group of individual objects as a single entity where insertion order preserved and duplicates are allowed, then we should go for List.

  • insertion order preserved.
  • duplicate objects are allowed.
  • heterogeneous objects are allowed.
  • Growable in nature
  • Values should be enclosed within square brackets.

List is dynamic because based on our requirement we can increase the size and decrease the size.

In List the elements will be placed within square brackets and with comma separator.

We can differentiate duplicate elements by using index and we can preserve insertion order by using index. Hence index will play very important role.

Python supports both positive and negative indexes. +ve index means from left to right where as negative index means right to left.


List1 = [3,8,7.2,”Hello”]

print(“List1[2] = “, List[2])

Output:  List1[2] = 7.2

print(“List1[1:3] = “, List[1:3])

Output: List1[1:3] = [8, 7.2]


list is growable in nature. i.e based on our requirement we can increase or decrease the size.



>>>[,”Hello”, “CloudNClear”]

An ordered, mutable, heterogenous collection of elements is nothing but list, where duplicates also allowed.

Accessing Elements of List:

We can access elements of the list either by using index or by using slice operator(:)

1) By using Index:

List follows zero based index. ie index of first element is zero.

List supports both +ve and -ve indexes.

+ve index meant for Left to Right

-ve index meant for Right to Left

list = [10, 20, 30, 40]


print(list[0]) à 10

print(list[-1]) à 40

print(list[10]) à IndexError: list index out of range


2) By using Slice Operator:

Syntax: list2 = list1[start:stop:step]

Start à It indicates the Index where slice has to Start

Default Value is 0

Stop à It indicates the Index where slice has to End

Default Value is max allowed Index of List ie Length of the List

Step à increment value

Default Value is 1

1) n=[1,2,3,4,5,6,7,8,9,10]

2) print(n[2:7:2])

3) print(n[4::2])

4) print(n[3:7])

5) print(n[8:2:-2])

6) print(n[4:100])




[3, 5, 7]

[5, 7, 9]

[4, 5, 6, 7]

[9, 7, 5]

[5, 6, 7, 8, 9, 10]


Important Functions of List:

1) len():

Returns the number of elements present in the list

Eg: n = [10, 20, 30, 40]

print(len(n) à 4

2) count():

It returns the number of occurrences of specified item in the list

1) n=[1,2,2,2,2,3,3]

2) print(n.count(1))

3) print(n.count(2))

4) print(n.count(3))

5) print(n.count(4))







3) index():

Returns the index of first occurrence of the specified item.

1) n = [1, 2, 2, 2, 2, 3, 3]

2) print(n.index(1)) à 0

3) print(n.index(2)) à 1

4) print(n.index(3)) à 5

5) print(n.index(4)) à ValueError: 4 is not in list


If the specified element not present in the list then we will get ValueError.Hence before index() method we have to check whether item present in the list or not by using in operator.

print( 4 in n) à False


4)  append() Function:

We can use append() function to add item at the end of the list.

1) list=[]

2) list.append(“A”)

3) list.append(“B”)

4) list.append(“C”)

5) print(list)



[‘A’, ‘B’, ‘C’]


6) insert() Function:

To insert item at specified index position

1) n=[1,2,3,4,5]

2) n.insert(1,888)

3) print(n)



[1, 888, 2, 3, 4, 5]


7) remove() Function:

We can use this function to remove specified item from the list.If the item present multiple times then only first occurrence will be removed.

1) n=[10,20,10,30]

2) n.remove(10)

3) print(n)



[20, 10, 30]


8) pop() Function:

It removes and returns the last element of the list.

This is only function which manipulates list and returns some element.

1) n=[10,20,30,40]

2) print(n.pop())

3) print(n.pop())

4) print(n)




[10, 20]


9) reverse():

We can use to reverse() order of elements of list.

1) n=[10,20,30,40]

2) n.reverse()

3) print(n)



[40, 30, 20, 10]


10) sort():

In list by default insertion order is preserved. If want to sort the elements of list according to default natural sorting order then we should go for sort() method.

For numbers à Default Natural sorting Order is Ascending Order

For Strings à Default Natural sorting order is Alphabetical Order

1) n = [20,5,15,10,0]

2) n.sort()

3) print(n) à [0,5,10,15,20]


5) s = [“Dog”,”Banana”,”Cat”,”Apple”]

6) s.sort()

7) print(s) à [‘Apple’,’Banana’,’Cat’,’Dog’]

This is all an about LIST Data Structure in PYTHON and its usage.

Related Posts:

Python Tutorial – Learn Python

What is Python? What makes Python so Powerful?

Variables in Python – Constant, Global & Static Variables

Namespacing and Scopes in Python

Operators in Python

Data Types in Python

STRING Data Type in Python

TUPLE Data Structure in PYTHON

Differences between List and Tuple

SET Data Structure in PYTHON


Database Programming in Python

What is Multithreading in Python?

Python Exception Handling Using try, except and finally statement

File Handling in Python

Python Random module

Python reduce() Function

Python map() Function

Python filter() Function

Lambda Function in PYTHON (Anonymous Function)

Python Interview Question And Answers