List in Python
Dr.M.Aniji
Data Structures in Python
Lists, Tuples, Dictionaries and Data
Frames are the important data
structures available in Python.
All of them are often used now for
analyzing data.
Lists
A list is an ordered sequence of comma-separated values of
any data type.
The values in a list are written between square brackets. A
value in a list can be accessed by specifying its position in the
list.
The position is known as index.
Here is an example.
list1 = [1, 2, 3, 4, 5]
list1 [0]
1
Mutable
The lists are mutable. This means that the
elements of a list can be changed at a later
stage, if necessary.
list2 = [10, 12, 14, 18, 30, 47]
list2[0] = 20
list2
[20, 12, 14, 18, 30, 47]
Index of list
Each element of a List can be accessed via an index, as we have seen
above.
The elements in a List are indexed from 0. Backward indexing from –1 is also
valid.
L[0] is “Michael Jackson”
L[1] is 10.1
L[2] is 2018
L[–1] is 2018
L[–2] is 10.1
L[–3] is “Michael Jackson”
L[1:3] will provide the list [10.1, 2018]
Syntax
A list can be created by using the list( ) function, as shown below:
list3 = list (<sequence>)
Here, the sequence shall be a list or string or tuple. Examples:
list4 = list (“hello”)
list4
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
list5 = list ((‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)) # Tuple
list5
[‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
Reading in the elements of a List
through the keyboard
We can use the list( ) method to read in the elements of a list
through the keyboard, as
shown below:
list6 = list (input(“Enter the elements of the List: “))
Enter the elements of the List: 1 2 3 4 5
list6
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
the integers that we have entered as input are represented as
strings
Eval()
to enter a list of integers or fractional values through the
keyboard, we have to use the eval( ) method,
marks = list(eval(input (“Enter marks obtained in 3 Maths,
Physics, Chemistry:”)))
Enter marks obtained in Maths, Physics, Chemistry: [92, 74, 83]
percentage = list(eval(input(“Enter percentage marks: “)))
Enter percentage marks: [92.0, 74.0, 83.0]
List Operations
joining lists,
replicating lists
slicing lists
Joining Two Lists
Joining two lists is very easy just like you perform addition
literally. The concentration
operator +, when used with two Lists, joins the two Lists.
Here is an example:
list7 = [1, 2, 4]
list8 = [5, 7, 8]
list7 + list8
[1, 2, 4, 5, 7, 8]
Replicating (i.e. Repeating) Lists
As in the case of Strings, you can use the *
operator to replicate a List for a specified
number of times.
Example:
list7 * 3
[1, 2, 4, 1, 2, 4, 1, 2, 4]
Slicing the Lists
As in the case of Strings, a List Slice is a part of a List, that is extracted
out of it.
Example:
list8 = [10, 15, 20, 25, 30, 35, 40, 45, 50]
seq1 = list8[3:6]
seq1
[25, 30, 35]
Note that the slice of a list contains the elements falling between
the indices 3 and 6, not including 6 (i.e., it contains the elements at
the positions 3, 4, and 5).
Recall what you have learnt about the negative indexing.
In the above example, the negative index of the element 50
is –1 and the negative index of 40 is –3.
So, the above List Slice shall also be obtained as shown
below:
list9 = [10, 15, 20, 25, 30, 35, 40, 45, 50]
seq2 = list9 [3 : –3]
seq2
[25, 30, 35]
List Methods- index() method
Python offers many built-in functions and methods for
performing List manipulations.
1. The index( ) method returns the index of the first
matched item from the list.
Example:
list10 = [13, 18, 11, 16, 18, 14]
list10.index(18)
1
append( ) method
2. The append( ) method adds a value to the end of the
List.
Example:
colors = [‘red’, ‘green’, ‘blue’]
colors.append (‘yellow’)
colors
[‘red’, ‘green’, ‘blue’, ‘yellow’]
extend() method
While the append( ) method adds just one element to the List, the
extend( ) method can add multiple elements to a List.
t1 = [‘a’, ‘b’, ‘c’]
t2 = [‘d’, ‘e’]
t1.extend (t2)
t1
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
t2
[‘d’, ‘e’]
insert( ) method
While the append( ) method and the extend( ) method
insert the element(s) at the end of the List, the insert( )
method inserts an element somewhere in between or
any position of your choice.
Example:
t1 = [‘a’, ‘e’, ‘u’]
t1.insert (2, ‘i’)
t1
[‘a’, ‘e’, ‘i’, ‘u’]
pop( ) method
The pop( ) method removes an element from a given position in the List
and returns it. If no index is specified, this method removes and returns the
last item in the List.
Example:
t2 = [‘k’, ‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘u’]
ele1 = t2.pop()
ele1
‘k’
t2
[‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘u’]
ele2 = t2.pop( )
ele2
‘u’
t2
[‘a’, ‘e’, ‘i’, ‘p’, ‘q’]
remove( ) method
The pop( ) method removes an element whose position is given. But, what
if you know the value of the element to be removed, but you don’t know its
index (i.e. position) in the List.
Python provides the remove( ) method for this purpose. This method
removes the first occurrence of given item from the List.
Example:
t3 = [‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘a’, ‘q’, ‘p’]
t3.remove (‘q’)
t3
[‘a’, ‘e’, ‘i’,’p’, ‘a’, ‘q’, ‘p’]
clear( ) method
The clear( ) method removes all the items from the given List. The List
becomes an empty List after this method is applied to it.
Example:
t4 = [2, 4, 5, 7]
t4.clear( )
t4
[]
count( ) method
The count( ) method returns the number of times given
item occurs in the List.
Example:
t5 = [13, 18, 20, 10, 18, 23]
t5.count (18)
2
reverse( ) method
The reverse( ) method just reverses the items in a List. It
does not return anything.
Example:
t6 = [‘e’, ‘i’, ‘q’, ‘a’, ‘q’, ‘p’]
t6.reverse ( )
t6
[‘p’, ‘q’, ‘a’, ‘q’, ‘i’, ‘e’]
sort( ) method
The sort( ) method sorts the items in a List in the ascending order by default. It
does not return anything. If we want this method to sort the items in the
descending order, we have to include the argument reverse = True.
Example:
t7 = [‘e’, ‘i’, ‘q’, ‘a’, ‘q’, ‘p’]
t7.sort( )
t7
[‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘q’]
t7.sort (reverse = True)
t7
[‘q’, ‘q’, ‘p’, ‘i’, ‘e’, ‘a’]