(iv) List in Python
(iv) List in Python
XI IP 2020-21
Lists: list operations - creating, initializing, traversing and manipulating
What is a
CONTENTS
LIST
Lists Intro: ? • A list is a collections of items which are
formed by placing a comma-separated-
List Definition list of expressions in square brackets.
Indexing in a List • Each item/element has its own index
Creation of a list
Traversal of a list. number.
Operations on a list - • Index of first item is 0 and the last item
Functions/methods
Lists Slicing;
is n-1. Here n is number of items in a
Nested lists; list.
finding the maximum, • Lists are mutable sequences i.e. we can
minimum, mean
linear search on list change elements of a list in place.
counting the frequency • Lists can contain values of mixed data
types.
INDEXING in a LIST
CONTENTS FORWARD INDEX DIRECTION
0 1 2 3 4 5
Lists Intro: 10 20 30 40 50 60
List Definition
Indexing in a List -6 -5 -4 -3 -2
Creation of a list BACKWARD INDEX
Traversal of a list -1
Operations on a list
Functions/methods DIRECTION
Lists Slicing; NOTE:-
Nested lists; The index (also called subscript) is the numbered
finding the maximum, position of a letter in the LIST. In python, indices begin
minimum, mean
from
linear search on list
counting the frequency 0,1,2,…. upto (length-1) in the forward
direction and
-1,-2,-3,….. (-length) in the backward
direction.
CREATION of LIST
CONTENTS
Lists Intro:
List of numbers created without name
Lists Intro:
List Definition
Indexing in a List
Creation of a list Creating list by taking input from the keyboard(user)
from string,tuple,
list(input()),
eval(input())
Traversal of a list **Note: All the elements are considered as string
Assign/change values in List
Operations on a list Creating list by taking input from the keyboard [using eval()]
Functions/methods ]method]
Lists Slicing;
Nested lists;
finding the maximum,
minimum, mean **Note: eval( ) identifies the type by looking at the given
linear search on list expression
counting the frequency
TRAVERSAL of a LIST
CONTENTS
Lists Intro:
List Definition
Indexing in a List
Creation of a list
Traversal of a list
Assign /change values in List
Operations on a list
Functions/methods
Lists Slicing;
Nested lists;
finding the maximum,
minimum, mean
linear search on list
counting the frequency
TRAVERSAL of NESTED LIST
CONTENTS
Enclosing List
Lists Intro:
List Definition
Indexing in a List Nested List 1 Nested List 2
Creation of a list
Traversal of a list
Assign /change values in List
Lists Intro:
List Definition Assigning
/ Changing
Indexing in a List values of
Creation of a list elements
Traversal of a list of a List
Assign /change
values in List
Operations on a list
Functions/methods
Lists Slicing; Assigning /
Nested lists; Changing
finding the maximum, values of
elements of
minimum, mean a List
linear search on list
counting the frequency
OPERATIONS on LIST Concatenation
CONTENTS List CAN be
concatenated or
Lists Introduction: joined with another
List CANNOT be concatenated with a
List
OPERATIONS number
ON List
Concatenation/Joining
Repetition/Replication
Membership List CANNOT be concatenated with a
Comparison of Lists string
Functions/methods
Lists Slicing;
Nested lists;
finding the maximum,
minimum, mean List CANNOT be joined with a complex
linear search on list nos.
counting the frequency
Concatenation
refers to Joining List CAN be
of two objects concatenated or
joined with another
OPERATIONS on LIST Replication
CONTENTS Replication refers to Repetition of objects
Lists Introduction:
as it is. Here, in LIST we can see NON-
VECTORISED operation with a list where
OPERATIONS the entire list as a whole is replicated.
ON List
Concatenation/Joining
NOTE: Vectorised operation, refers to
the replication of individual elements of
Repetition/ the object.
Replication Replicating the unnamed List two
Membership times
Comparison of Lists
Functions/methods
Lists Slicing;
Nested lists;
finding the maximum, Replicating the named List RL four
minimum, times
mean
linear search on list
counting the frequency
OPERATIONS on LIST Membership
Membership operator tests for Membership in a sequence.
CONTENTS Returns Boolean True or False
Lists Introduction: OPERATOR DESCRIPTION
OPERATIONS
in Results to True value if it finds a variable at the LHS of in
ON List operator in the sequence mentioned in the RHS of the in
operator, else it returns False
Concatenation/Joining
Repetition/ not in Results to True value if it does not find a variable at the
LHS of in operator in the sequence mentioned in the RHS
Replication of the in operator, else it returns False
Membership
in List
Comparison of Lists
Functions/methods
Lists Slicing;
Nested lists;
finding the maximum,
minimum, mean
linear search on list
counting the frequency
OPERATIONS on Comparison of LIST
CONTENTS Comparison of list returns Boolean True or False.
Functions/
methods
len(),
list(),append(),
extend(), insert(),
count(),index(), remove(),
pop(), reverse(), sort(),
min(), max(),
sum(),clear() TOTAL 5 elements of the List object
Lists Slicing;
Nested lists;
finding the maximum,
Functions / Methods on LIST- list()
The list() function creates a list object.
CONTENTS A list object is a collection which is ordered and
changeable.
Lists Introduction:
list() function
Operations on a list takes
Functions/ elements/values
inside the
methods parantheses i.e. ()
len(),
list(),append(),
extend(), insert(),
count(),index(), remove(),
pop(), reverse(), sort(),
min(), max(),
sum(),clear()
Lists Slicing;
Nested lists; NOTE: Python language is case-sensitive.
finding the maximum, Here, Z is the List object , so, z in small letters gives an error.
Functions / Methods on LIST-append()
CONTENTS The append() function takes one single parameter
and adds exactly one element at the end of the List.
Lists Introduction: It takes exactly one element and returns no value.
Operations on a list
Functions/
methods Here, append() function takes one single
parameter . In this example, 30.
len(),
list(),append(),
extend(), insert(),
count(),index(), remove(),
pop(), reverse(),
sort(),min(), max(),
sum(),clear()
Lists Slicing;
Nested lists;
finding the maximum,
minimum,
Functions / Methods on LIST-extend()
CONTENTS The extend() function adds iterable (Eg. Another List/
Lists Introduction:
Tuple/String) at the end of the List.
Operations on a list
Functions/methods Here, trying to extend list A , with another
len(), list(),append(), unnamed List containing one element [30].
extend(), insert(), Here, trying to extend list A , with
count(),index(), remove(),
another unnamed List containing
pop(), reverse(), sort(),
three elements [4,50,77].
min(), max(), sum(),clear()
Lists Slicing; Here, trying to extend list A , with
Nested lists; another named List B containing
finding the max,min, three elements
mean *‘know’,’python’,’bytes’+
linear search on list
counting the frequency
Here, trying to extend list A , with a number 10. Hence error !!!
Here, trying to extend list A , with an unnamed tuple with two values
Lists Slicing;
Nested lists;
finding the maximum,
minimum, mean
linear search on list
counting the
frequency
Functions / Methods on LIST- reverse()
CONTENTS
Lists Introduction:
Operations on a list reverse( ) method,
Functions/ reverses the items of the list.
This doesn’t create a new list.
methods It takes no argument and return no list.
len(), list(),append(),
extend(), Reverses the list ‘in place’ and does not
insert(),count(),index(), return anything.
remove(), pop(),
reverse(), sort(),min(),
max(), sum(),clear()
Lists Slicing;
Nested lists;
finding the maximum,
minimum, mean
linear search on list
counting the frequency
Functions / Methods on LIST- sort()
CONTENTS sort( ) method,
Lists Introduction: sort( ) method and sort(reverse=True ) sorts or
Operations on a list orders the items of the list, by default in increasing order.
sort(reverse=True ) for getting the list in decreasing order
Functions/
methods
len(), list(),append(),
extend(),
insert(),count(),index(),
remove(), pop(),
reverse(),
sort(),min(), max(), NOTE: The sort( ) method
sum() , clear() won’t be able to sort the
Lists Slicing; values of a list if it contains
Nested lists; complex numbers/tuple
finding the / mixed datatype as its
elements.
maximum,
minimum,
Functions / Methods on LIST- min(),max(),sum()
CONTENTS min( ) method,
Lists Introduction: Returns the minimum out of the elements/values/items
Operations on a list of the given List
Functions/ max( ) method,
methods Returns the maximum out of the elements/values/items
len(), list(),append(), of the given List
extend(),
insert(),count(),index(), sum( ) method,
remove(), pop(),
Returns the sum of all the values/elements/items of
reverse(),
the given List.
min(),
sort(),
max(), sum()
,
clear()
Lists Slicing;
Nested lists;
finding the
Functions / Methods on LIST- min(),max(),sum() contd..
List of string . Maximum
and Minimum is done on
the basis of the ASCII
values of the characters
from the beginning.
Eg. ‘A’=65, ‘a’ = 97, ‘b’=98
and so on
A List with
mixed data
type cannot
return values
for the min,
max and
sum
functions.
Functions / Methods on LIST- clear()
CONTENTS
Lists Introduction:
Operations on a list clear( ) method,
Functions/ removes all the items from the list and
methods the list becomes empty after executing
len(), list(),append(), this function.
extend(),
insert(),count(),index(),
remove(), pop(),
reverse(),
sort(),min(), max(),
sum() ,
clear()
Lists Slicing;
Nested lists;
finding the
maximum,
LIST slicing
L[start:stop] creates a list slice out of list L with elements falling between
indexes start and stop, not including value at the stop index.
Start index 5, stop index is 50 which is beyond the last index in forward
direction. Hence, it will stop at the last index
Start index -10(which is ‘Y’ as per the backward index,) stop index is 12-1=11
(forward indexing). Hence, it will stop at the (stop index-1)., ‘Y’ is at 11 index no.
LIST SLICING contd.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
K N O W P Y T H O N B Y T E S
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Start index -3(backward), stop index is -12(backward). Here, it is not getting
any value in the range as no step value mentioned in reverse. Hence empty.
Start index -12(which is ‘W’ as per the backward index,)
stop index is -3 (which takes values till index -3-1= -4)
Default start and stop is beginning and end index of the list. Here, step is 3.
NESTED LISTS
CONTENTS Enclosing List
Lists Intro
Operations on a list
Functions/methods
Nested List 1
Lists Slicing;
minimum, mean
linear search on list
counting
Note: the frequency
Denoting Nested elements
using Index nos.
M[0] 10 M[1]
[15,30]
M[1
]
[0]
15
M[1][1]30
M[2] *80,’Mask’,60+
Program to find the MAXIMUM element from a list of element along
with its index in the list without using max() library function
CONTENTS
Lists Intro
Operations on
a list
Functions/
methods
Lists
Slicing;
Nested
lists;
finding
the
maximu
m OUTPUT
, minimum,
mean
linear search
on list
counting the
Program to find the MINIMUM element from a list of element along
with its index in the list without using min() library function
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
Lists
Slicing;
Nested
lists;
finding
the
minimu
m, OUTPUT
maximum ,
mean
linear search
on list
counting the
Program to find the MEAN (AVERAGE) of all the elements of
the list
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
Lists
Slicing;
Nested
lists;
finding
the mean,
maximum,
minimum
linear search OUTPUT
on list
counting the
frequency
Program to search an element in the List (LINEAR SEARCH)
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
Lists Slicing;
Nested lists;
finding the
mean, maximum,
minimum
linear
search on
list
counting the
frequency OUTPUT
Program to count frequency (/no. of times of occurrence) of an element in the List
(FREQUENCY COUNT)
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
Lists Slicing;
Nested lists;
finding the
mean, maximum,
minimum
linear search
on list
counting
the OUTPUT
frequency
Bibliograhy and References
• Google
• Wikipedia
• XI Computer Science , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
• XI Informatics Practices , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
Programming is
an ART….
Add your colours to it and
make your own