List
Features of List
In Python List consider as a mutable data type i.e. Values in the list can be
modified.
In Python there are two ways to declare a list variable:
(a) Using [ ] bracket
(b) Using list ( ) pre-defined function
A List which contains no element is called Empty list.
A List is a collection of values or an ordered sequence of values/items.
The items in a list can be of any type such as string, integers, float or even a list.
Elements of a list are enclosed in a square bracket.
Each list element is separated by commas.
2
Features of List
In Python we can identify each list element by their index location.
In Python we can access individual element of a list using positive index
location as well as negative index location.
We can perform following operations on list variable:
(a) Concatenation
(b) Replication
(c) Traversing
(d) Slicing
3
Declaring / Creating a List
In Python there are two ways to declare a list variable :
(a) Using [ ] bracket : List variable can be created by putting comma-separated
values/items within square bracket. The syntax for creating a list is
listvariablename = [ element1, element2, element3, element4…..]
4
Declaring / Creating a List
(b) Using list ( ) pre-defined function : This function takes sequence types and
convert a given sequence into a list. If no sequence is specified then it will create
an empty list.
5
Accessing List Elements
A list consists of any collection of items which are stored according to its
index. Like strings, any item in list can be accessed by using its indexvalue.
List indices start with 0. The list index can be a positive or a negative integer
value or an expression which evaluates to an integer value. Hence, indexing
in lists can be positive or negative, i.e. you can access the elements from a
list either moving from left to right (positive index) or from right to left
(negative index). The index of -1 refers to the last item, -2 to the second last
item and so on. For example, consider list1containing 10 elements:
list1=[10,20,30,40,50,60,70,80,90,100]
6
Operations on Lists
Concatenation : Concatenation is a process in which multiple
sequences/lists can be combined together with help of + operators. Python
allows combining or adding of two lists using ‘+’ concatenation operator.
Replication : Multiply ( * ) operator replicates the list for a specified
number of times.
7
Operations on Lists
Traversing : Traversing a list means accessing each element of a list. This
can be done by using either for or while looping statement.
8
Operations on Lists
Slicing : Indexing and Slicing are two inter-related operations in lists.
Slicingis an operation in which we can slice a particular range from that
sequence. List slices are the sub-part of a list extracted out. List slices can
be created through the use of indexes. Slicing is used to retrieve a subset of
values. A slice of a list is basically its sub-list, while indexing is used to
obtain individual items, slicing allows you to obtain subset of items. When
you enter a range you want to extract, it is called range slicing. The syntax
for slicing is: listvariable[start:stop:step]
9
Operations on Lists
10
Membership Testing
Membership testing is an operation carried out to check or test whether a
particular element/item is a member of that sequence or not. ‘in’ operator
can be applied to check whether a individual character or element available
in 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. The ‘not in’ operator returns true if element does not appear in
the sequence, other wise returns false.
11
Pre-Defined Functions / Methods / Statement
Pre-Defined Functions : list ( ) , len( ) , sorted( ), min( ), max( ), sum ( )
Pre-Defined Methods :
Regarding insertion : insert( ), append( ), extend( )
Regarding Deletion : remove( ), pop( ), clear( )
count( ), index( ), reverse( ), sort( )
Pre-Defined Statement :
del
12
Pre-Defined Functions
len( ) function : This function is used to find total number of elements
length of the list variable.
sorted( ) function : This function is used to arrange list elements in
ascending or descending order. By default it will arrange list element in
ascending order.
13
Pre-Defined Functions
min( ) function : This function is used to find minimum number from a
given list.
max( ) function : This function is used to find maximum number from a
given list.
sum ( ) function : This function is used to find sum of all elements from a
given list.
14
Pre-Defined Methods
append ( ) : This method is used to add a single element at the end of the
list. It does not create a new list; rather it modifies the original list. Syntax
of append( ) method is : [Link](element)
insert( ) : This method is used to insert a single element in the list at the
specified index location. Syntax of insert() method is:
[Link](indexlocation, element)
15
Pre-Defined Methods
extend ( ) : This method is used to adds or insert each element to the end of
the list. Element should be specified in the form of list.
16
Pre-Defined Methods
remove ( ) : This method is used to when we know the element to be
deleted not the index of the element.
pop( ) : This method is used to remove the element from the specified
index, also returns the element which was removed. By default pop( )
method remove last element from the given list if the index location is not
specified.
17
Pre-Defined Methods
clear ( ) : This method is used to removes all the elements from the given
list and list becomes empty.
del statement : This statement is used to remove the specified element
from the list according to specified index location but does not return the
deleted value.
18
Pre-Defined Methods
count ( ) : This method is used to count the frequency of a given element in
a given list.
Index( ) : This method is used to find the index location of a specified
element from a given list.
19
Nested List
20
How to input list element at the run time?
21
To find sum of all list elements without using sum( ) function
22
List of Programs
1) Write a Python code to declare (different types) and print all elements of a
list.
2) Write a Python code to print for adding and removing elements from a
list.
3) Write a Python code to input a list and add an element at the specified
index.
4) Write a Python code to input a list and remove an element from the
specified index location.
5) Write a Python code to input a list and remove an element from the list
according to specified name.
6) Write a Python code to input a list and count the frequency of a given
element without using count( ) method.
7) Write a Python code to input a list and calculate the sum and mean of a
given list of numbers. 23
List of Programs
8) Write a Python code to input a list and display even and odd elements
separately from a given list.
9) Write a Python code to input a list and search an input element in a given
list of numbers.
10) Write a Python code to input a list, replicates it twice and then prints the
sorted list in ascending and descending order.
11) Write a Python code to input a list and count total even and odd elements
separately from a given list.
12) Write a Python code to input a list of integers and replace elements
having odd values with thrice their value and elements having even
values with twice their value.
13) Write a Python code to input a list of integers and divide all the list
elements by 7 which are divisible by 7 and multiply other elements by 3.
24