Lists in Python
CLASS 11
CBSE
Characteristics of DATA TYPES
Immutable Mutable
Non-modifiable - DT which can Modifiable
never change value in place
integers, floating point, Booleans, Lists, dictionaries, sets
strings, Tuples
The variable names are stored as ref. In the same memory address, new value
to a value-object. Each time we can be stored multiple times.
change the value, the variable’s
memory address change.
DATA TYPES
Data Type for Range
NUMBERS
Integers (IM) (int) , an unlimited range. Signed representation (i.e. it can be
either +ve or -ve)
Booleans (IM) Subtype of plain integers. It takes only two values True (1) or
False (0)
Floating point numbers Double precision floating point numbers (15 digit precision)
(IM)
Complex Numbers A pair of floating point numbers A + Bj , the real and imaginary
(IM) part.
z.real gives the real part and
z.imag gives the imaginary part as a float, not as a complex value
DATA TYPES
Other Data Types Range
Strings (IM) Sequence of any type of known characters and each character can be
individually accessed using index
Tuples (IM) Group of comma-separated values of any data type within
parentheses.
E.g., a = (5,2,9,4) or („h‟, „k‟, „s‟) or (8, 3, „A‟, „W‟)
Lists (M) Group of comma-separated values of any data type between square
brackets. Index starts from 0 and so on
E.g., a = [5,2,9,4] or [„h‟, „k‟, „s‟] or [8 , 3.45 , „A‟ , „W‟, „Miyu‟]
Sets (M) Similar to lists, but between the curly brackets. It can take values
of different types but cannot contain mutable elements and
duplicate entries.
E.g., a = {5, 3, 8, 4}
Dictionaries (M) Unordered set of comma-separated Key : Value pairs within { } .
Every key is Unique. Eg.
{„a‟ : 1, „e‟ : 2, „i‟ : 3, „o‟ : 4}
What are Lists?
List is a collection / ordered sequence of values / items /elements. It is a
sequenced data type.
The elements in a list can be of any type such as string, integers, float,
objects or even a list.
It allow duplicate values
Lists are Mutable i.e., values in the list can be modified
Elements in a list need not be of the same type. Lists are heterogeneous
(of multiple types)
Declaring/ Creating/ Initializing List
Syntax of creating a list
<list_name> = [ ]
for eg. L=[] #Initialization of the list / Empty list
flowers = [‘Rose’, ‘Lotus’, ‘Jasmine’]
Declaring/ Creating/ Initializing List
List Types
Empty List | Long List | Nested List
L1 = [10, 20, 30, 40, 50] #list of integers
L2 = [10, „Sun‟ , 31.5, “Moon”,-554, True, False] #list of with different datatypes
L3=[„a‟, „e‟, „I‟, „o‟, „u‟] #list of characters
L4 = [„Rose‟, „Lotus‟, „Jasmine‟] #list of 3 strings
L5 = [3, 4, [5, 6, 7], 8] #list containing another list
Declaring/ Creating/ Initializing List
Creating a List from an existing sequence
list() method takes sequence types and converts a record/tuple/string into list
1) An empty list created
L2 = list() #L2 [ ]
2) Creating a list from a sequence (string)
L2=list(‘comp’) #L2 [‘c’, ‘o’, ‘m’, ‘p’]
3) Creating a list through user input
L2=list(input(„Enter the values: ‟)
>>>Enter the values: python
print(L2)
[„p‟, „y‟, „t‟, „h‟, „o‟, „n‟]
Declaring/ Creating/ Initializing List
Creating a List from an existing list
L1 = [10, 20, 30, 40, 50]
L2 = L1[ : ] L2 is copy of L1
L2 = L1[ 1 : 4] L2 contain elements at index 1, 2, 3 from L1
L2 = [20, 30, 40]
L2 = L1 L2 has all the elements present in L1
Accessing List Elements (Indexing)
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
L1 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
L1[2] = 30
Traversing a List
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Using ‘in’ operator inside for loop
Using range() function
Using while loop
Traversing a List
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Traversing a list means accessing each element of a list
Using ‘in’ operator inside for loop
Using range() function
Using while loop
1) Using ‘in’ operator inside for loop
#program #backend
L1 = [„a‟, „e‟, „I‟, „o‟, „u‟] for 0 in L1[o] > print(0) > a
for i in L1: for 1 in L1[1] > print(1) > e
print(i) ----------
0 1 2 3 4 for 4 in L1[4] > print(4) > u
a e i o u
-5 -4 -3 -2 -1
1) Using ‘in’ operator inside for loop
#write a program to find the sum of all elements in a list
List1 = [10,2,4,5]
sum=0
for i in List1:
sum = sum +1
print(“the elements in the list are”, List1)
print(“the sum is: “, sum)
2) Using range() function
#program to count total no. of characters #backend
L1 = [„p‟, „y‟, „t‟, „h‟, ‟o‟] for 0 in range[5] > print(0) > p
for 1 in range[5] > print(1) > y
n = len(L1) #to count total no. characters
----------
for i in range(n): #n being the stop point for 4 in L1[5] > print(4) > o
print(L1(i)) for 5 in L1[5] > abort / end
print(“Total no. of characters are: “)
print(n)
• range() checks for the set of values mentioned as the
0 1 2 3 4
argument to range() function. It is used for accessing
p y t h o individual elements of a list, using indexes of elements
-5 -4 -3 -2 -1 only along with len() method which gives length of the
list and specifies how many times for loop will
iterate
3) Using while function
#program to traverse list using while loop O<5 -- L1[0] ---p index=0+1=1
L1 = [„p‟, „y‟, „t‟, „h‟, ‟o‟] 1<5 -- L1[1] ---y index=1+1 =2
index = 0 --------------
4<5 ----L1[4] ---0 index=4+1=5
while index < len(L1):
5<5 ----- abort
print(L1(index))
index += 1
• While loop starts from index() and is iterated through
0 1 2 3 4
the list items
p y t h o • Unlike a for loop, it is must to increase the index
-5 -4 -3 -2 -1 by 1 after each iteration as shown
Aliasing
Lists are objects, they are mutable / modifiable. If we assign the elements of one
list to another list, both shall refer to the same object.
>>> sub = [„hindi‟, „end‟, „maths‟, „history‟]
>>> temp = sub
#both temp and sub refer to same objects / address.
#temp is a an alias (alternate) name for sub list as „=„ doesn‟t make a copy; instead makes /n
both these lists referring to the same location. Changes made with one alias get reflected in
the other
>>> temp[0] = „French‟
>>> print (temp)
[„French‟, „end‟, „maths‟, „history‟]
>>> print (sub)
[„French‟, „end‟, „maths‟, „history‟]
>>> Subjectcodes=[ [„hindi‟, 40], [‟eng‟, 25] ,[‟maths‟, 30], [‟history‟, 50] ]
>>>Subjectcodes[1]
[‟eng‟, 25]
>>>Print(subjectcodes[2][0])
[„maths‟]
>>>Sum = subjectcodes[0][1] + subjectcodes[1][1] + subjectcodes[2][1] + subjectcodes[3][1]
#Sum = 40 + 25 + 30 + 50
List is represented in two-dimensional manner. In order to access their
individual element from the nested list, we have to mention the row and column
of index value.