A List in Python represents a list of comma separated values of any data
types between square brackets i.e. [ ]. A list is a mutable data type. i.e. a
user is allowed to change any existing value stored with in a list. Just like
other normal variables a list is also stored in a variable the declare and
stored the list in a variable is as follow…
ListVariable = [Value1, Value2, Value3,…, ValueN]
e.g. Here variables A, B are list
types variables. Variable A
contains Empty List,
where as variable B
contains mixed data
values in it as list.
2
A List in Python represents a list of comma separated values of any data
types , lets have a look at the different types of lists based on the data
values stored in it.
Example Type Of List
L=[] Empty list
L=[1 , 2 , 3] List of integers
L=[1 , 2.5 , 4.5 , 6] List of numbers (integer and floating
point numbers)
L=[‘a’ , ’b’ , ’c’] List of single characters
L=[‘a’ , 3.5 , ’b’ , 8 , ’Sarla’] List of mixed data values
L=[‘Rahul’ , ’Amit’ ,Sarla’] List of strings
3
A List in Python is represented and accessed in memory by adding an
additional integer number called an index or subscript. Python supports
two ways indexing or subscripting techniques, namely forward indexing
and backward indexing. In forward indexing each data value in a list will
be assigned an index starting from 0 in left to right order. the last data
value in a list value will have index as list length-1. Consider following
example.
e.g. X= [10,”CD”,”A”,12.5,5,6]
LEFT RIGHT
Data Values
10 CD A 12.5 5 6
Forward
Indexing 0 1 2 3 4 5
4
In backward indexing each data value in a list value will be assigned an
index starting from -1 in right to left order. the last data value in a list
value will have starting index as -1. Consider following example.
e.g.
X= [10,”CD”,”A”,12.5,5,6]
LEFT RIGHT
Data Values
10 CD A 12.5 5 6
Backward
Indexing -6 -5 -4 -3 -2 -1
5
In Python when we access data value with in list then it must be with in
the forward or backward indices range. If we try to access the data value
from the list beyond it’s forward/ backward indices then Python will
generate index out of bound error. In Python to access individual data
value from the list we use ListVariable[ ], the square brackets accepts an
index of the data value with in a list value.
Here we try to access 6th
index element, which
doesn’t exists, So error is
generated. 6
In Python a list data type is mutable data type i.e. any data value exists
in the list variable can be changed as per requirement based on the
index / subscript specified. Consider following example.
Here B list is declared
with some data values,
after which 2nd index
element (3rd Value) is
changed to 20 which was
earlier “MN”. 7
In Python a list data type is mutable data type i.e. any data value exists
in the list variable can be changed as per requirement based on the
index / subscript specified. When we specify the range of indices to
update a range of values then all the elements on those indices will be
replaced with the new values. Consider following example.
Here indices 0 and 1 of list
variable L will be replaced
with “Rahul” and “Viraj”
8
In Python a list data type is mutable data type i.e. any data value exists
in the list variable can be changed as per requirement based on the
index / subscript specified. When we specify the range of indices to
update to a single values then all the elements on those indices will be
replaced with the new value. Consider following example.
Here indices 0 and 1 of list
variable L will be replaced
with value 90
9
In Python a list data type is mutable data type hence we can also
remove the elements exists in a list variable using del keyword. Using
del command we can either remove an element , range of elements and
entire list. Consider following list.
Here 1st index element will
be removed from list
variable L.
10
Here elements on indices
2 to 3 element will be
removed from list variable
L. 11
Here entire list will be
removed.
12
A List in Python can be created as per our requirement. A list can be
created by specifying values with in square brackets [ ] or by specifying
comma separated values with built in list() function. A list can be created
in one of the following forms.
Types of
Lists
Empty Long Nested
List Lists List 13
A. Empty List
An empty list is [ ]. It is the list equivalent to 0 or ‘’ and like them it
also has truth values as false. You can create an empty list using built
in list() function. Consider following example.
Here variable L is a list,
which is not initialized
with any single value.
Hence called empty list
which is equivalent to
false.
14
B. Long List
If a list contains many elements of any data types, then to enter such
long list you can split it into different lines. Consider following example.
Here variable L is a list,
which is initialized with
any multiple value. Hence
called long list which is
equivalent to true.
15
C. Nested List
If a list contains many elements out of which one or more elements
are again list itself, will be considered as nested list. Nested list is a
logical structure in which one list contains another list as its element.
Consider following example.
Here variable L is a list,
which contains one of
element is a list, called
nested list.
16
Remarks:
A list can also be created from any existing sequences like List,
tuples etc using build in list() function. Consider following example for
create list from a List value.
Here variable L is a list,
which contains individual
elements extracted or
converted from List value.
17
Remarks:
A list can also be created from any existing sequences like List,
tuples etc using build in list() function. Consider following example for
create list from a tuple value.
Here variable L is a list,
which contains individual
values extracted or
converted from tuple
value.
18
Remarks:
A list can also be created by obtaining values input() function during
run time and then pass these input to built in eval() function which
look for the each value entered through keyboard determine its type
and then store it with that type as en element with the list. Consider
following example.
Here variable L is
dynamically initialized,
using input function.
19
Remarks:
A list can also be created by obtaining values input() function during
run time and then pass these input to built in eval() function which
look for the each value entered through keyboard determine its type (it
may perform some calculations) and then store it with that type as en
element with the list. Consider following example.
Here variable L is
dynamically initialized,
using input function.
20
In Python a Traversing a list refers to process, visit each and every
element exists in a list value , starting from the 1st most element to the
last most element. In Traversing we can process one element of the list
at a time. The easiest way to traverse a list is to use for loop to iterate
through each element of the list . Consider following example.
Here for loop traverse in a
list variable L, each
element is processed
from 1st to last one.
21
A List expression in Python is a valid combination of list literals or
operands and any two arithmetic operators i.e. either + or *. When we use
+ operator between two literals and then the result will be the concatenated
lists. When we use * operator and one literal, operand will be list type
where as the other literal or operand will be of integer type only, it will
multiply each element by give literal. Consider the following examples.
Here L1 + L2 is a list expression
and the result will be
concatenation of two lists.
22
Here L1 * 3 or 3 * L1 is a list
expression and the result will be
repetition list L1 three times..
Here L1 * L2 are list expression ,
but this expression violating list
expression rules i.e. form
multiplication one literal/
variable must be list and other
must be integer. Therefore error
. 23
Membership operators are operators used to validate the membership
of a value. It test for membership in a sequence, such as Lists, lists, or
tuples. in operator : The ‘in’ operator is used to check if a value exists in
a sequence or not. Evaluates to true if it finds a variable in the specified
sequence and false otherwise. ‘not in’ operator- Evaluates to true if it
does not finds a variable in the specified sequence and false otherwise.
In list membership operators are case sensitive.
Here membership operators as
used to search a element
member of other lists or not.
24
Python’s standard comparison operator i.e. all relational operators
(<,>,<=,>=,!= & ==) can be applied to list also. The comparison using
these operators are based on the standard element-by-element
comparison. The == and != operators are easy to understands and
recognize as either true or false, but when comparison takes place
using <, >, <= and >= then the comparison will takes place using
standard element-by-element comparison rules for number and
Unicode encoding scheme (i.e. character’s ordinal value). Some
common elements and their ordinal values are as follows.
Elements Ordinal Values Range
0 TO 9 48 TO 57
A TO Z 65 TO 90
a to z 97 TO 122
25
Here Lists are compared using
== and != operators. These
operators are Case Sensitive for
List values.
Here ordinal value of ‘A‘ is 65
and for ‘a’ its is 97. Hence based
on this values L1=L2 will be
false and so on 26
Python’s standard built in function len() allows us to find total number of
elements exists with in list. Consider following example.
Len() function count total
numbers of elements
present in the list variable
L
27
A List Slices in Python refers to a part of the List which is extracted from
another List. It is a List containing some contagious element of another
List. In Python to have a List slice we must use the syntax as
ListVariable[ StartingIndex : EndlingIndex]. When we extract elements
using List slice, it start extracting from starting index elements fro
ending index -1 (i.e. ending index element is always excluded during
List Slices). In List Slices we can specify indices using any List indexing
technique i.e. forward indexing or backward indexing. Consider the
following List representation.
e.g.
X=list(‘ALAUDDIN’) 28
The representation of the above List using forward indexing inside
memory will as follows…
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
The representation of the above List using backward indexing inside
memory will as follows…
Elements
Backward
A L A U D D I N
-8 -7 -6 -5 -4 -3 -2 -1
Indexing
29
Here X[0 : 8] will extract all
elements between indies 0 to 7.
Extracting element LEFT To RIGHT order
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
30
Here X[0 : 8 : 2] will extract all
elements between indies 0 to 7
by skipping 1 element each time.
Extracting element LEFT To RIGHT order
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
31
Here X[0 : 3] will extract all
elements between indies 0 to 2.
Extracting Character
LEFT To RIGHT order
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
32
Here X[2 : 5] will extract all
elements between indies 2 to 4.
Extracting element
LEFT To RIGHT order
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
33
Here X[ : 7] will extract all
elements between indies 0 to 6.
If Starting index is missing then
Python will consider it as 0
Extracting element LEFT To RIGHT order
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
34
Here X[ : 5] will extract all
elements between indies 0 to 4.
If Starting index is missing then
Python will consider it as 0.
Extracting element LEFT To RIGHT order
Elements
Forward
A L A U D D I N
0 1 2 3 4 5 6 7
Indexing
35
Here X[-7 : -3] will extract all
elements between indies -4 to -7.
It will exclude element as index -
3. The order of extraction will be
always from LEFT TO RIGHT
Extracting element
LEFT To RIGHT order
Elements
Backward
A L A U D D I N
-8 -7 -6 -5 -4 -3 -2 -1
Indexing
36
Here X[ : : -1] will extract all
elements between indies 0 to 7
from RIGHT to LEFT i.e. reverse
a List.
Extracting element RIGHT TO LEFT order
Elements
Backward
A L A U D D I N
-8 -7 -6 -5 -4 -3 -2 -1
Indexing
37
Here X[ : : -2] will extract all
elements between indies 0 to 7
from RIGHT to LEFT i.e. reverse
a List, but this will skip one
element each time.
Extracting element RIGHT TO LEFT order
Elements
Backward
A L A U D D I N
-8 -7 -6 -5 -4 -3 -2 -1
Indexing
38
Here X[0:5] will extract all
elements between indies 0 to 4
from RIGHT to LEFT, but the List
is empty and python will not
generate any error.
Extracting element RIGHT TO LEFT order
Elements
Forward
Indexing
0 1 2 3 4 5 6 7
39
Python also offers many built in functions and methods for List
manipulation. Every List object you create in Python is actually an
instance of List class. The List class allows us to use readymade
methods to perform some simple and miscellaneous tasks. To use List
function using List object we must use the following syntax.
[Link](Arguments/Parameters)
The built in function offered by List class in Python are as follows…
index() append() extend() insert() pop()
remove() clear() count() reverse() sort()
40
A. index()
This function returns the index of the first occurrence of the searched
value from existing list. If the value doesn’t exists the this function will
generate an error. Consider following example.
index() searches index of first
occurrence of an element 10.
41
index() searches index of first
occurrence of an element 10
which doesn’t exists therefore
error will be generated.
42
B. append()
This function add a new item(s) at the end of an existing list. You can
add any type of value in this method to append it to existing list.
Consider following example.
append() function adds 20 adds
it in existing list variable L.
43
append() can not accept multiple
values as arguments hence error
will be generated.
44
append() can not accept multiple
values as arguments hence error
will be generated. But if multiple
values are specified as tuple or
list or dictionary then it will add
it at the end.
45
C extend()
This function accepts a list of values and add it’s the end if existing list. It
is very useful when we want to add multiple elements with in existing list.
This function will not accept a single value, it accepts single value that
too in a form of list. Consider following examples.
extend() function add multiple
values at the end of existing list.
46
extend() function can not add a
single value hence error will be
generated.
47
D insert()
This function adds a new value of any data type with in existing list
variable on a specified index. It accepts two arguments namely index
and value. Index is integer value where we need to add a new element.
In case an index specified doesn’t exists then new value will be inserted
at the end of existing list.
Here, “Rahul” is inserted on 3rd
index in existing list variable L
48
Here, index specified is out of
forward index bounds hence
“Rahul” value will be inserted at
the end of existing list.
49
Here, index specified is out of
bounds of backward index
hence “Rahul” value will be
inserted at the beginning of
existing list.
50
D pop()
This function accept an index of an value and removes it from existing
list variable. Index as an argument is optional to this method. In case
index is missing during this function call then last most element will be
removed. While removing item it also return the item being removed as a
result. If no elements are there of index is out of bounds then error will
be generated.
pop() function accepts an index
and removes it from list.
51
pop() function accepts an index
and removes it from list. The
index doesn’t exists hence error
will be generated.
52
pop() function doesn’t accepts
any index. Hence it will remove
last element.
53
E remove()
This function is also similar to pop() function, but unlike pop() this
function accepts a value to be removes searches it in existing list and if
matched then removes first occurrence it from existing list variable. In
case the value to be removes doesn’t exists then error will be generated.
Consider following examples.
remove() function accepts one
value and removes first
occurrence of the specified
value from existing list.
54
remove() function accepts one
value and removes first
occurrence of the specified
value from existing list. But the
value doesn’t exists then error
will be generated.
55
F clear()
This function removes all the values from existing list variable and
makes it as an empty list.
clear() function removes all
items from existing list variable
L
56
G count()
This function returns total number of occurrences of specified value
exists in a list variable. If the value specified doesn’t exists then the this
function will return 0. Consider following examples.
count() function count total
occurrences of specified value
from list variable L
57
count() function count total
occurrences of specified value
from list variable L, but the value
specified doesn’t exists in L
hence return 0.
58
H reverse()
This function reverses the list values specified in existing list variable.
This function changes the order from LEFT-Right To RIGHT-LEFT in
place itself, i.e. it doesn’t return anything. When reverse is executed the
forward and backward indices will be changed for all elements.
Consider following example.
reverse() reverses he order of
values with in list variable L
59
I sort()
This function arrange the elements either in ascending or descending
order with the existing list. It doesn’t return anything. By default the order
of arrangement is in ascending. To arrange list in descending order we
need to pass reverse=True, as its argument. After invoking this method
the forward and backward indices will be changed.
sort() arranges values of the list
variable L in ascending order.
60
sort() arranges values of the list
variable L in descending order.
61