List in Python
List in Python
Class : XI (2024-25)
ShriKant Yadav
PGT (Info. Pract.)
Chapter : 5
Lists in Python
Introduction
• In Python, a list is a kind of container that contains
collection of any kind of values.
• A List is a mutable data type which means any value
from the list can be changed. For changed values ,
Python does not create a new list.
• Long lists-
This is tuple
even = [0, 2, 4, 6, 8, 10 ,12 ,14 ,16 ,18 ,20 ]
• Nested list -
L = [ 3, 4, [ 5, 6 ], 7]
Another method
Creation of List
-As we have seen in the
example That when we have
supplied
values as numbers to a list even then
They have automatically converted to string
– If we want to pass values to a list in numeric form then we have to write
following function -eval(input()) L=eval(input(“Enter list to be added “))
eval ( ) function identifies type of the passed string and then return it.
Another example
String Values
Accessing a List
• First we will see the similarities between a List and a String.
• List is a sequence like a string.
• List also has index of each of its element.
• Like string, list also has 2 index, one for forward indexing (from
0, 1, 2, 3, ….to n-1) and one for backward indexing(from -n to -
1).
• In a list, values can be accessed like string.
Forward index 0 1 2 3 4 5 6 7 8 9 10 11 12 13
List R E S P O N S I B I L I T Y
Backward index -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Accessing a List
• len( ) function is used to get the length of a list.
Important 1:membership
operator (in, not in) works
in list similarly as they work
in other sequence.
Important 2: + operator
adds a list at the end of
other list whereas *
operator repeats a list.
Difference between a List and a String
• Main difference between a List and a string is that string is
immutable whereas list is mutable.
• Individual values in string can’t be change whereas it is
possible with list.
Value didn’t
change in string.
Error shown. Value got changed
in list specifying
list is mutable
Traversal of a list
• Traversal of a list means to access and process each and
every element of that list.
• Traversal of a list is very simple with for loop –
for <item> in <list>:
Some
examples
Last item
6th item
1st item
List Functions and Methods
– Python provides some built-in functions for list manipulation
– Syntax is like <list-object>.<method-name>
Function Details
List.extend(<list>) Append the list (passed in the form of argument) at the end of list
with which function is called.
List.pop(<index>) Delete and return the element of passed index. Index passing is
optional, if not passed, element from last will be deleted.
List.remove(<value>) It will delete the first occurrence of passed value but does not
return the deleted value.
List Functions and Methods
Function Details
List.clear ( ) It will delete all values of list and gives an empty list.
List.count (<item>) It will count and return number of occurrences of the passed element.
List.reverse ( ) It will reverse the list and it does not create a new list.
List.sort ( ) It will sort the list in ascending order. To sort the list in descending
order, we need to write----- list.sort(reverse =True).
List Functions and Methods
• List.index ( ) function:
• List.append( ) function:
• List.extend( ) function:
• List.insert( ) function:
List Functions and Methods
• List.pop ( ) function:
• List.remove( ) function:
• List.count( ) function:
List Functions and Methods
• List.reverse( ) function:
• List.sort( ) function:
Important
• To sort a list in reverse order, write in following manner–
List.sort(reverse=True)