LISTS IN PYTHON
The Python lists are containers
that are used to store a list of
values of any type.
Python lists are mutable
(modifiable). Elements of a list
are enclosed in square
brackets[ ], separated by
commas .
CREATING/DECLARING LIST
Lists can be created by
putting comma
separated values within
square brackets.
Syntax
Listname=[values]
L1=[ ] #empty list
L2=[1,2,3,4,5] #list of integers
L3=[‘a’,’b’,’c’,’d’] #list of characters
L4=[“apple”,”mango”,”grapes”] #list of strings
L5=[20.3,4.6,67.8] #list of floating point numbers
L6=[1,2,’a’,”apple”,56.89] #list of mixed data types
Lists can be
displayed by typing
the list name in the
python shell prompt
PYTHON SHELL PROMPT
PYTHON IDLE
output
CREATING A LIST FROM AN
EXISTING SEQUENCE
The list() method in Python takes sequence types
and convert a given sequence in to a list.
output
Input() function
Creating list using input
output
Here all elements entered is string even though we
entered digits
To enter a list of integers as input, use int()
function
output
ACCESSING LIST ELEMENTS
List elements have index. List indices start with
zero. It can be positive or negative integer
value. We can access the list from left to right
or from right to left.
L1=[‘H’,’E’,’L’,’L’,’O’]
0 1 2 3 4 POSITIVE
INDEX
H E L L O LI
-5 -4 -3 -2 -1 NEGATIVE
INDEX
0 1 2 3 4 POSITIVE INDEX
H E L L O LI
-5 -4 -3 -2 -1 NEGATIVE INDEX
L1[0] = L1[-5] = ‘H’
L1[1] = L1[-4] = ‘E’
L1[2] = L1[-3] = ‘L’
L1[3] = L1[-2] = ‘L’
L1[4] = L1[-1] = ‘O’
ACCESSING THE LIST USING FOR LOOP
output
USING FOR LOOP PRINTING THE OUTPUT IN
THE SAME LINE
output
DISPLAYING THE MEMBERS OF THE LIST WHICH
CONTAINS STRINGS
output
output
ASSIGNING CONTENTS TO TWO DIFFERENT LISTS
output
EXAMPLE TO SHOW THAT LISTS ARE MUTABLE
output
ALIASING THE LIST
output
Both the lists are referring to the same
location
subjects Eng Hindi Mal Sanskrit
0 1 2 3
temp
Comparing Lists
Lists can be compared if they are of the same
type. Corresponding element must be compared
and if it is equal, it goes to the next element for
comparison, until it finds elements that differ.
Comparison result
[1,2,8,9] < [9,1] true
[1,2,8,9] < [1,2,9,1] true
[1,2,8,9] < [1,2,9,10] true
[1,2,8,9] > [1,2,9,4] false
[1,2,3] == [1,2,3] true
[1,2,4] != [1,2,3] true
BASIC LIST OPERATIONS
1.CONCATENATION
2. REPETITION
3. MEMBERSHIP TESTING
4. SLICING
5. INDEXING
CONCATENATION (JOINING LISTS)
Concatenation Operation can be done only with
two lists. The operator used is +.
REPETITION/REPLICATION
OPERATOR USED IS *
MEMBERSHIP TESTING
Membership testing is an operation carried
out to check or test whether a particular
element is a member of that sequence or
not. The in operator checks whether a given
element is contained in a list. It returns true
if element appears in the list, otherwise
returns false.
INDEXING
There is an index value for each element in the
list. Indexing starts from 0. An index is a number
specifying the position of an element in a list.
Negative index identify positions to the end of the
list. The index -1 identifies the last element, -2
identifies the second last element and so on.
SLICING
Slicing is done through indexing.
Syntax
L[start : stop] creates a list slice out
of the list L with elements falling
between indexes start and stop
[not including stop]
Lists also support slice steps
S1=l1[start : stop : step]
When double colon is coming then it is step
no.
BUILT IN FUNCTIONS
BUILT – IN FUNCTIONS
FUNCTION SYNTAX EXAMPLE
append() L1.append(item) L1=[10,20,30,40]
Adds element at the L1.append(50)
end of the list print(L1)
[10,20,30,40,50]
extend() L1.extend(L2) L1=[10,20,30]
Adds one list at the L2=[40,50]
end of another list L1.extend(L2)
print(L1)
[10,20,30,40,50]
insert() L1.insert(index , L1=[10,20,40]
To insert an element value) L1.insert(2,30)
at a specified index print(L1)
[10,20,30,40]
Function Syntax example
reverse() L1.reverse() L1=[10,20,30,40]
Reverses the order of the L1.reverse()
elements in the list. print(L1)
[40,30,20,10]
index() L1.index(item) L1=[10,20,30,40]
Gives the index of the first print(L1.index(20))
matched item in the list 1
len() len(L1) L1=[10,20,30,40]
Gives the length of the list print(len(L1))
4
sort() L1.sort() L1=[4,6,2,3,8]
Sorts the list in ascending L1.sort()
order print(L1)
[2,3,4,6,8]
l1.sort(reverse=True)
L1.sort(reverse=True)
print(L1)
[8,6,4,3,2]
Function Syntax example
count() L1.count(element) L1=[10,20,30,10,40]
Counts how many print(L1.count(10))
times an element 2
has occurred in a
list
clear() L1.clear() L1=[10,20,30]
Removes all the L1.clear()
items in the list print(L1)
[]
max() max(L1) L1=[5,7,8,2,3]
print(max(L1))
8
min() min(L1) L1=[5,7,8,2,3]
print(min(L1))
2
Function Syntax example
remove() L1.remove(elemen L1=[10,10,20,30]
Removes the first t) L1.remove(10)
occurrence of print(L1)
item from the list [10,20,30]
pop() L1.pop()
Removes the Removes the last
element from the element in the list.
specified index L1.pop(index)
and also return the Removes the
element which element from the
was removed specified index
sorted() in list will return a new
sorted list .Original list remain
unchanged.
Differentiate sort() and sorted()
function in list
sort() will sort the original list in
ascending order whereas sorted() will
return a new sorted list leaving the
original list unchanged.
The del keyword removes the element
specified by the index or deletes the
whole list.
THANK YOU