Python Data Structures - Lists and Tuples | Brain Mentors Skip to content

Python Data Structures – Lists and Tuples

List

In Python , the list is a data structure which contains an ordered sequence of elements. List is mutable (Which means we can create, edit or delete the items of the list).In List, You can store all types( number, string, boolean ) of data. inside list.

As like String can be written using single/double inverted Quotes, List is written using []

list1 = [ ” B ” , ” R ” , ” A ” , ” I ” , ” N ” ]

Indexing in List

Index is the position which can be used to get a specific item from the list.Here, Indexing in List starts from 0, then 1 , then 2 , then 3 , then 4 and so on. and if we start from last index of list, then indexing starts from -1, then -2 , then -3 ….so on.

python-indexing

Using Index , we can retrieve any element from the list .
Now, Element located at nth index can be find by writting list name followed by square brackets, and inside square bracket, we have to write the index for which we want item corresponding to that index.

List Slicing

List Slicing is the process to make sublist from a given list.

Example : Let’s suppose , there is a list say mylist = [1,2,3,4,5,6,7,8,9,10] , and we have to make a sublist from it by taking out the first five numbers from given list.

Syntax for List Slicing : listname [start index : end index : step size]

List Slicing

Reverse the Whole list using list slicing

List can be reversed by traversing from last ,this can be done using step size (-1) . Here , we will start from last index , then with step size of -1, we got indexes like -1,-2,-3,-4,-5 ….so on. and finally we will get our reversed List.

List Comprehension

mylist1 = [i for i in range(1,11)]

Here, A loop will be started from 1 and goes upto 10 , and all numbers from 1 to 10 will go inside mylist1

Methods in List

a. append Method :

append method is used to insert the elements at the end of the list.

b. insert method :

insert method is used to insert the element at any position in the list. we just have to mention index inside insert method at which we want to insert a value.

c. pop method :

pop method is used to remove element located at last index of the list .Also it can be used to remove element located at a specified index

Now,if we want to remove an item which is located at 1st index, then pass the index number inside the pop function , and it will remove the item which is located at that index .

d. remove method :

remove method is used to an item from the list.Simply we have to pass that item in the remove method which is to be deleted.

Note : del keyword is also used for removing items from the list.

Also , del keyword can be used to delete the entire list

e. extend method :

This method is used to extends the list by adding all items of a list (passed as an argument) to the end of another list.
The syntax of extend() method is:
list1.extend(list2)
Here, the elements of list2 are added to the end of list1.

f. sort method :

This method is used to sort the list either in ascending order or descending order.
The syntax of sort() method is:
list1.sort()
Here , List will be sorted in increasing order

In order to sort the list in descending order, inside sort function , we have to set reverse=True

g. count method :

The count() method returns the frequency of an item in the list.

h. copy method :

Copy method is used to copy an list to another list.

Tuple

like List , Tuple is also a data structure which contains ordered sequence of elements. Here, The main Difference between list and tuple is that Tuple is immutable (only creation is possible here

, editing the items in the tuple is not possible).List is written using []. Tuple is written using ().

tuple1 = ( 1 , 2 , 3 , 4 , 5 )

As like List, Indexing and Slicing is as it is same in Tuple also

Tuple is immutable, which means Once we create a Tuple, Neither its items be changed nor we can delete items in it.

Sign Up and Start Learning