0% found this document useful (0 votes)
24 views71 pages

List

Uploaded by

geezy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views71 pages

List

Uploaded by

geezy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Computational Thinking

© CS-DEPT DPS MATHURA ROAD


and Programming - 2
Lists

XI
Definition
A Python list is a standard data type of Python that can store a sequence of
elements belonging to any type. Python lists are mutable, i.e. you can change the

© CS-DEPT DPS MATHURA ROAD


elements of a list in place.
Each element of a list can be individually accessed using its index. These
elements can be accessed using either forward indexing or backward indexing.

You can access any element as <listname>[index]. The index can be a forward
index or a backward index.
➢ 0, 1, 2,.... In the forward direction
➢ -1, -2, -3….. In the backward direction

2
XI
Examples of Lists
Examples of Lists are:

© CS-DEPT DPS MATHURA ROAD


L=[] # Empty list
Number = [10,20,30,40] # List of integer values
Values = [1.2,5.6,8,9,123.8] # List of float values
Names = ['Arun', 'Sheetal',’Mohit'] # List of strings

# List of different types of data


Student = [10,'Akshay Rawat',99.5] # List containing int, string and float values

3
XI
Indexing

© CS-DEPT DPS MATHURA ROAD


Forward Indexing

0 1 2 3 4 5

list Red Blue Green White Grey Pink


-6 -5 -4 -3 -2 -1

Backward Indexing

4
XI
Indexing
The elements in a list are ordered (numbered), starting from 0.

© CS-DEPT DPS MATHURA ROAD


It means that the first element of the list is numbered 0, the second element is
numbered 1, third is numbered 2, and so on.

These numbers are called indices of list elements.

Python supports negative indices also.

In case of negative indices, -1 is the index for the last element, -2 for the second
last element, and so on.

5
XI
Creating Lists
To create a list, put a number of values in square brackets.

© CS-DEPT DPS MATHURA ROAD


In general we can create a list in the form given below:

L=[] # creating an empty list

L = [value1, value2,....] #creating list with values

Example :

L1=[1,2,3,4,5] # list containing elements of the same type

L2=[1,”RAHUL”,80.0] # list containing elements of mixed data types

6
XI
Creating Lists (Contd.)
A list can be created from a sequence as per the syntax:

© CS-DEPT DPS MATHURA ROAD


L = list(<sequence>)
where sequence can be any kind of sequence object including strings, tuples and
lists.

Consider the following examples:


>>>L1 = list(“PYTHON”)
>>>print(L1)
[‘P’,’Y’,’T’,’H’,’O’,’N’]

7
XI
We can also create a list from single characters or single digits entered via the

© CS-DEPT DPS MATHURA ROAD


keyboard.
>>>L1 = list(input(“Enter the values of the list:”))
Enter the values of the list:123456
>>>L1
[‘1’,’2’,’3’,’4’,’5’,’6’]
>>> s1=eval(input(“enter the values of a list”))
[10,20,30,40]
>>> s1
[10, 20, 30, 40]
8
XI
Types of Lists
● Empty list :The empty list is a list containing no values.

© CS-DEPT DPS MATHURA ROAD


You can create an empty list as:
L = list() or L=[]
● Long lists : If a list contains many elements, then to enter such long lists, you
can split them across several lines such as:
List1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
● Nested lists : A list can have an element in it, which itself is a list.
Such a list is called a nested list, such as:
List2 = [1,2,[3,4],5]

9
XI
Accessing elements of List:
● By using the list name and index numbers.

© CS-DEPT DPS MATHURA ROAD


For example:
color = ['Red', 'Green', 'Pink', 'Black', 'Orange']
print (color[2]) displays 'Pink'.
print (color[-2]) displays 'Black'.
print(color[6]) error
Note : If you give an index outside the legal indices ( 0 to length-1 or -length,
-length+1), Python will raise IndexError

10
XI
Accessing Elements of List:
● By using for loop where the range is specified by the list name.

© CS-DEPT DPS MATHURA ROAD


For example:
color = ['Red', 'Green', 'Pink', 'Black', 'Orange']
for i in color:
print(i, end=' ')

OR

color = ['Red', 'Green', 'Pink', 'Black', 'Orange']


for i in range ( 0, len(color)):
print(color[i], end=' ')

11
XI
Strings vs Lists
Similarity Difference

© CS-DEPT DPS MATHURA ROAD


Lists are sequences just like Strings are not mutable while
strings. lists are mutable.

They also index their individual Individual elements of the string


elements just like strings do. cannot be changed while the
elements of the lists can be
We can have forward indexing as
changed.
0,1,2,3,4………… and backward
indexing as -1,-2,-3,-4……..

12
XI
Operations on Lists
O

© CS-DEPT DPS MATHURA ROAD


P 1 Concatenation
E
R
A
2 Replication
T
I 3 Membership
O
N 4 Slicing
S
13
XI
Concatenation
The + operator adds one list to the end of the other list. This operator requires

© CS-DEPT DPS MATHURA ROAD


both the operands to be of list type. You cannot add a number/ complex number /
string.
Syntax : listname1+listname2

l1 = [10,20,30] OUTPUT
Example l2 = [40,50,60]
print("List1 :") List1 :
print(l1) [10,20,30]
print("List2 :") List2 :
print(l2) [40,50,60]
l3 = l1 + l2 [10,20,30,40,50,60] 6
print(l3, len(l3))
14
XI
Replication
The * operator as a replication operator needs two operand i.e a list and an integer.

© CS-DEPT DPS MATHURA ROAD


Python replicates the list elements the given number of times.

Syntax : listname * integer data item

OUTPUT
l1 = [10,20,30] [10,20,30,10,20,30]
Example print(l1*2)

15
XI
Comparison
All relational and comparison operators (==, >, <, <=, >=, !=) can be used to
compare two lists .

© CS-DEPT DPS MATHURA ROAD


Python internally compares individual elements of the lists in lexicographical order.
This means that to check if the two lists are equal, each corresponding element is
compared and the two sequence must be of the same type.
Examples

[1,2,8,9] < [9,1] Returns True when comparing corresponding


first element of two lists :
1< 9 is True
[1,2,8,9] < [1,2,9,1] Returns True when comparing corresponding
third element of two lists :
8 < 9 is True
16
XI
Membership
The operators in and not in are used to check whether element exists in the given

© CS-DEPT DPS MATHURA ROAD


list or not.
The in operator returns True if an element exists in the given list; False otherwise.
The not in operator returns True if an element does not exist in the given list; False
otherwise.
Syntax : element in <listname>
element not in <listname>

Example l1 = [10,20,30] OUTPUT


print(l0 in l1)
True
print(33 in l1)
False
print(23 not in l1)
True
17
XI
List Slicing
List slicing refers to retrieve few elements from the list which falls between two

© CS-DEPT DPS MATHURA ROAD


indexes.To extract the slice two indexes are given in square bracket separated by a
colon (:) .

Syntax : listname [start : end : step]

where start, end and step are integers


start represents the starting index of list
end denotes the end index of list which is not inclusive
step denotes the distance between the elements to be sliced

18
XI
Slicing of lists
list1 = [1, 2, 3, 5, 6, 7, 10, 11,12]

© CS-DEPT DPS MATHURA ROAD


list2=list1[2:6]
print("List after slicing :") OUTPUT
print(list2)
List after slicing :
[3,5,6,7]
Lists also supports slice steps.

list1 = [1, 2, 3, 5, 6, 7, 10, 11, 12] OUTPUT


list2=list1[ 1 : 8 : 2 ]
print("List after slicing with step :") List after slicing with
print(list2) step:
[2,5,7,11]
19
XI
Slicing for modification
Examples

© CS-DEPT DPS MATHURA ROAD


OUTPUT
list1=["one", "two", "THREE"]
list1[0:2]=[0,1]
List after
print("List after modification:")
modification :
print(list1)
[0, 1, 'THREE']
If the range of the list slice is much outside the length of the list,
it will simply add the values at the end.
OUTPUT
list1=[ 10, 20, 30]
list1[ 10 : 20 ] = "abcd" List after modification:
print("List after modification:") [10, 20, 30, 'a', 'b', 'c', 'd']
print(list1)
20
XI
Exercise :
Find the output of the following statements :

© CS-DEPT DPS MATHURA ROAD


A=[ 3, 6, 7, 8, 10, 12, 23, 33, 16, 6, 2, 1 ]

(i) print(len(A)) (ii) print(A[3])


(iii) print(A[-3]) (iv) print(A[3:])
(v) print(A[-3:]) (vi) print(A[::3])
(vii) print(A[3::]) (viii) print(A[3::-2])
(ix) print(A[-3::-2]) (x) print(A[::-3])
(xi) print(A[3:-3:3]) (xii) print(A[3:-3:-3])
(xiii) print(len(A[3:])) (xiv) print(len(A[-3:])
(xv) print(len(A[::-3]))

21
XI
Solutions :

© CS-DEPT DPS MATHURA ROAD


(i) 12 (viii) [8, 6]
(ii) 8 (ix) [6, 33, 12, 8, 6]
(iii) 6 (x) [1, 16, 12, 7]
(iv) [8, 10, 12, 23, 33, 16, 6, 2, 1] (xi) [8, 23]
(v) [6, 2, 1] (xii) [ ]
(vi)[3, 8, 23, 6] (xiii) 9
vii)[8, 10, 12, 23, 33, 16, 6, 2, 1] (xiv) 3
(xv) 4

22
XI
List Functions and Methods

© CS-DEPT DPS MATHURA ROAD


SORTED
MAX

SUM LIST
FUNCTIONS

MIN
LEN

23
XI
List Functions and Methods
1. len()

© CS-DEPT DPS MATHURA ROAD


Returns the length of the list or the number of elements in the list

Syntax : len(listname)

OUTPUT

Length : 5
Example

list1=[10,20,30,40,50]
print("Length:",len(list1))

24
XI
List Functions and Methods
2. max()

© CS-DEPT DPS MATHURA ROAD


Returns the largest element of the list. All the elements must be of the same
data type.

Syntax : max(listname)
OUTPUT

Maximum: 78
Example

list1=[1,45,34,11,78]
print("Maximum:",max(list1))

25
XI
List Functions and Methods
3. min()

© CS-DEPT DPS MATHURA ROAD


Returns the smallest element of the list. All the elements must be of the same
data type.

Syntax : min(listname)
OUTPUT

Minimum: 11
Example

list1=[100,45,34,11,78]
print("Minimum:",min(list1))

26
XI
List Functions and Methods
4. sorted()

© CS-DEPT DPS MATHURA ROAD


This function returns the sorted values in ascending order (by default) or
descending order in the list. All the values in the list must be of the same data
type.
Syntax : sorted(<list>,<reverse = True/False (default)>)

OUTPUT
Example
list1=[1,45,34,11,78] Sorted List:
print("Sorted List:") [1,11,34,45,78]
print(sorted(list1))
27
XI
List Functions and Methods
5. sum()

© CS-DEPT DPS MATHURA ROAD


This function returns the sum of all elements in the list.

Syntax : sum(<listname>)

OUTPUT
Example
list1=[10,20,30,40,50] Sum:
print("Sum:") 150
print(sum(list1))
28
XI
List Functions and Methods
6. append()

© CS-DEPT DPS MATHURA ROAD


The append() method adds a single item to the end of the list.

Syntax : listname.append(element)

OUTPUT
Example
List :[1,100,78]
list1=[1,100,78] After appending :
print("List :",list1) [1,100,78,88]
list1.append(88)
print("After appending :")
print(list1)
29
XI
List Functions and Methods
7. extend()

© CS-DEPT DPS MATHURA ROAD


It is used for adding multiple elements to the list. It takes a list as an argument and
appends all the elements of the argument list to the list object on which extend() is
applied.
Syntax : listname.extend(<listname>)

OUTPUT
Example list1=[1,100,78] List :[1,100,78]
list2=[10,11,12] After extending :
print("List :",list1) [1,100,78,10,11,12]
list1.extend(list2)
print("After extending :")
print(list1)
30
XI
List Functions and Methods
8. insert()

© CS-DEPT DPS MATHURA ROAD


It is used to insert an element somewhere in between/ any specified position in
the list.
Syntax : listname.insert(<pos>,<element>)

Example OUTPUT
list1=[1,100,78]
print("List :",list1) List :[1,100,78]
list1.insert(2,90) After inserting :
print("After inserting :") [1,100,90,78]
print(list1)

31
XI
insert() method
Note : If the pos index is greater than the len(list) (i.e. the index is out of bound),

© CS-DEPT DPS MATHURA ROAD


the object is simply appended to the list.

l1=['a','e','o','u'] OUTPUT
print('List :',l1)
l1.insert(8,'z') List :['a','e','o','u']
print('After inserting') After inserting :
print(l1) ['a','e','o','u','z']

32
XI
insert() method

© CS-DEPT DPS MATHURA ROAD


Note : If the pos index is less than zero or not equal to any of the valid negative
indexes of the list, the object is prepended to the list (added at the beginning of
the list)
l1=['a','e','o','u'] OUTPUT
l1.insert(-9, 'xyz' )
print('After inserting') After inserting :
print(l1) ['xyz', 'a', 'e', 'i', 'o', 'u', 'z']

33
XI
List Functions and Methods
7. pop()
It is used to remove an item from the specified position in the list. If no

© CS-DEPT DPS MATHURA ROAD


index is specified then pop() removes the last element from the list. It gives
an error in case there is no element at the index specified.

Syntax : listname.pop(<index>)

Example OUTPUT
list1=["x","y","z"]
print("List:",list1) List:["x","y","z"]
x=list1.pop(1) After popping :
print("After popping:") ["x","z"]
print(list1)

34
XI
List Functions and Methods
8. remove()

© CS-DEPT DPS MATHURA ROAD


It removes the first occurrence of the given item from the list.
If the item to be deleted is not present in the list, Python generates an
exception.

Syntax : listname.remove(<value>)

Example l1=[1,2,3,4,3,2,1] OUTPUT


print("List:",l1)
l1.remove(3) List:[1,2,3,4,3,2,1]
print("After removing:") After removing :
print(l1) [1,2,4,3,2,1]

35
XI
List Functions and Methods
9. clear()

© CS-DEPT DPS MATHURA ROAD


It is used to remove all the items from the list and the list becomes empty
after the function. The list object still exists as an empty list.

Syntax : listname.clear()

Example OUTPUT
l1=[1,2,3,4,3,2,1] List:[1,2,3,4,3,2,1]
l1.clear() After clearing :
print("After clearing:") [ ]
print(l1)

36
XI
List Functions and Methods
10. count()

© CS-DEPT DPS MATHURA ROAD


It returns the count of the item that has been passed as an argument. If the
item is not present in the list, it returns zero.

Syntax : listname.count(value)

Example OUTPUT
l1=[11,22,33,44,33]
print("List:",l1) List:[11,22,33,44,33]
x=l1.count(33) After counting :
print("After counting:") 2
print(x)

37
XI
List Functions and Methods
11. reverse()

© CS-DEPT DPS MATHURA ROAD


It reverses the items of the list without creating any new list.

Syntax : listname.reverse()

Example OUTPUT
l1=[4,3,3,2,2,1,1]
print("List1:",l1) List1:[4,3,3,2,2,1,1]
l1.reverse() After reversing :
print("After reversing:") [1,1,2,2,3,3,4]
print(l1)

38
XI
del statement
It also be used to remove individual items/ all items identified by the slice from the

© CS-DEPT DPS MATHURA ROAD


list. If the del statement is used as del List, it deletes all the items as well as the list
object.

Syntax : del listname[index]

Example OUTPUT
l1=[1,2,3,4,3,2,1]
print("List1:",l1) List:[1,2,3,4,3,2,1]
del l1[4] After deleting :
print("After deleting:") [1,2,3,4,2,1]
print(l1)

39
XI
Program #1
Program to calculate the average of the list of values entered by the user

© CS-DEPT DPS MATHURA ROAD


l=eval(input("Enter list:")) Output :
length=len(l)
Enter list:[3,5,1,6,3]
m=s=0 Given list is: [3, 5, 1, 6, 3]
for i in range(length): Sum of elements is : 18
s+=l[i] Average of list is : 3.6
m=s/length
print("Given list is:",list)
print("Sum of elements is : ",s)
print("Average of list is : ",m)

40
XI
Program #2
Program to find the maximum element from a list of numbers entered by the user

© CS-DEPT DPS MATHURA ROAD


along with the index of the largest element
l=eval(input("Enter list:")) Output :
length=len(l)
mx=l[0] Enter list:[3,5,1,6,3]
ind=0 Given list is: [3, 5, 1, 6, 3]
for i in range(1,length): Maximum element 6 at index 3
if l[i]>mx:
mx=l[i]
ind=i
print("Given list is:",l)
print("Maximum element ",mx,"at index ",ind)

41
XI
Program #3
Program to enter a list of values and a number x to be searched. Search for x in the

© CS-DEPT DPS MATHURA ROAD


list. If present, display the position where found else display the message “element
is not present in the list”
l=eval(input("Enter elements of the list:")) Output :
length=len(l)
x=int(input("Enter the number")) Enter elements of the list:
for i in range(length):
[1,4,5,26,33]
if x==l[i]:
print("Element found at ",i,"position") Enter element to be
break searched:
else:
print("Element is not present in the list") 26

Element found at position 4

42
XI
Program #4
Program to input N numbers from the user and find their sum and average. After that

© CS-DEPT DPS MATHURA ROAD


display all those numbers (entered by the user) which are greater than the average.
list1=[] Output :
s=0
Enter the limit :5
N=int(input("Enter the limit :"))
Enter the number :11
for i in range(N): Enter the number :12
num=int(input("Enter the number :")) Enter the number :13
list1=list1+[num] Enter the number :14
s=s+num Enter the number :15
avg=s/N Sum : 65
print("Sum :",s) Average : 13.0
Numbers which are greater
print("Average :",avg)
than average:
print("Numbers which are greater than average:")
14
for i in list1: 15
if i>avg:
print(i) 43
XI
Program #5
Program to count the frequency of a given element in a list of values.

© CS-DEPT DPS MATHURA ROAD


l1=eval(input("Enter the list:")) Output :
element=int(input("Enter element to be searched :"))
c=0 Enter the list:[1,2,3,4,4,5,4]
for i in range(len(l1)): Enter element to be searched :4
if element==l1[i]: 4 has frequency of 3 in given list
c+=1
if c==0:
print(element," not found in given list")
else:
print(element," has frequency of ",c," in given
list")

44
XI
Program #6
Program to input a list, named Pay, from the user and modify each element of Pay,
as per the following rules:

© CS-DEPT DPS MATHURA ROAD


Existing value of Pay Pay to be changed to
If less than 100000 Add 25% in the existing value
If >= 100000 and <200000 Add 20% in the existing value
If >= 200000 Add 15% in the existing value

45
XI
Program #6
pay=eval(input("Enter elements of the list:")) Output :

© CS-DEPT DPS MATHURA ROAD


print("Original List :",pay)
Enter elements of the list:
for i in range(len(pay)):
if pay[i]<=100000: [10000, 200000, 350000]
pay[i]=pay[i]+pay[i]*0.25
Original List :
elif pay[i]<=200000:
pay[i]=pay[i]+pay[i]*0.20 [10000, 200000, 350000]
else: Updated Pay :
pay[i]=pay[i]+pay[i]*0.15
[12500.0, 240000.0, 402500.0]
print("Updated Pay :",pay)

46
XI
Program #7
Program to input a list from the user and modify its content in such a way that the

© CS-DEPT DPS MATHURA ROAD


elements, which are multiples of 10 are swapped with the value present in the very
next position in the list.

For example If the content of list P is: 91, 50, 54, 22, 30, 54

Then content of list P should become: 91, 54, 50, 22, 54, 30

47
XI
Program #7
l1=eval(input("Enter elements of the list:")) Output :

© CS-DEPT DPS MATHURA ROAD


print("Original List :",list1) Enter elements of the list:[10,22,34,50,67]
i=0
Original List : [10, 22, 34, 50, 67]
while i<len(l1)-1:
Updated List : [22, 10, 34, 67, 50]
if l1[i]%10==0:
l1[i],l1[i+1]=l1[i+1], l[i]
i=i+2
else:
i=i+1
print("Updated List :",l1)

48
XI
Program #8
Program to input a list from the user and change all the multiples of 5 in the list to 5

© CS-DEPT DPS MATHURA ROAD


and the rest of the elements to 0.

For example, if the list contains: [55, 43, 20, 16, 39, 90, 83, 40, 48, 25]

Then after executing the program, the list content should be changed as follows:

[5, 0, 5, 0, 0, 5, 0, 5, 0, 5]

49
XI
Program #8
l1=eval(input("Enter elements of the list:")) Output :

© CS-DEPT DPS MATHURA ROAD


print("Original List :",list1) Enter elements of the list:[1,2,30,4,5,50]
for i in range(len(l1)):
Original List : [1, 2, 30, 4, 5, 50]
if l1[i]%5==0:
Updated List : [0, 0, 5, 0, 5, 5]
l[i]=5
else:
l[i]=0
print("Updated List :",l1)

50
XI
Program #9
Program to input a list of integers and replace each even element of the list with the

© CS-DEPT DPS MATHURA ROAD


sum of its digits and each odd element with the product of its digits.

For example, if the list is entered as: [100, 43, 20, 56, 32, 91, 80, 40, 45, 21]

After executing the program, the list content should be changed as follows:

[1, 12, 2, 11, 5, 9, 8, 4, 20, 2]

51
XI
Program #9
l=eval(input("Enter elements of the list:")) Output :
print("Original List :",l)

© CS-DEPT DPS MATHURA ROAD


for i in range(len(l)): Enter elements of the list:
num=l[i]
[100, 43, 20, 56, 32, 91]
s=0
p=1 Original List : [100, 43, 20, 56, 32, 91]
if l[i]%2==0:
while num>0: Updated List : [1, 12, 2, 11, 5, 9]
r=num%10
s=s+r
num=num//10
l[i]=s
else:
while num>0:
r=num%10
p=p*r
num=num//10
l[i]=p
print("Updated List :",l)
52
XI
Program #10
Write a program to input a list of names of students. Display the names of those

© CS-DEPT DPS MATHURA ROAD


students whose names end with ‘A’.
l=eval(input("Enter names of the students:")) Enter names of the students:
print("Names of students which ends with A :") ["Ria" ","Akshay","Gurshan","Priya"]
for i in range(len(l)):
Names of students which ends with A :
if l[i][-1] in "Aa":
print(l[i]) Ria

Priya

53
XI
Program #11

© CS-DEPT DPS MATHURA ROAD


Write a program to input a list of numbers and re-positions all the elements of the list by
shifting each of them one position ahead and by moving the last element to the first.

For example, if the list contains [25,30,90,11,16]

After changing the list content should be displayed as [16,25,30,90,11]

54
XI
Program #11
l=eval(input("Enter elements of the list:")) Output :

© CS-DEPT DPS MATHURA ROAD


print("Original List :",l) Enter elements of the list:[25,30,90,11,16]
length=len(l)
Original List : [25, 30, 90, 11, 16]
t=l[-1]
for i in range(length-2,-1,-1): Updated List : [16, 25, 30, 90, 11]
l[i+1]=l[i]
l[0]=t
print("Updated List :",l)

55
XI
Program #12
Write a program to swap the even and odd positions of the values in the list Val.

© CS-DEPT DPS MATHURA ROAD


Note : Assuming that the list has an even number of values in it.
For example :
If the list Numbers contain [25,17,19,13,12,15]
After swapping the list content should be displayed as [17,25,13,19,15,12]

56
XI
Program #12
l=eval(input("Enter elements of the list:")) Output :

© CS-DEPT DPS MATHURA ROAD


print("Original List :",l) Enter elements of the list:[25,17,19,13,12,15]
for i in range(0,len(l),2):
Original List : [25, 17, 19, 13, 12, 15]
l[i],l[i+1]=l[i+1],l[i]
print("Updated List :",l) Updated List : [17, 25, 13, 19, 15, 12]

57
XI
Output Questions
Consider the lists A and B given below and write the output of the statements that
follow:

© CS-DEPT DPS MATHURA ROAD


A=[5, 3, 8]
B=[A, 8, 66, 45, 'A',['A', A], ["nested", "lists"]]

(i) print(A) (vii) print(B[0][1])

(ii) print(B) (viii) print(B[0][1:])

(iii) print(A[0]) (ix) print(B[4])

(iv) print(A[0:]) (x) print(B[5])

(v) print(A[:0]) (xi) for i in A:


print(B[i%6])
(vi) print(B[0])
58
XI
Solutions
(i) [5, 3, 8] (vii) 3

© CS-DEPT DPS MATHURA ROAD


(ii) [[5, 3, 8], 8, 66, 45, 'A', ['A', [5, 3, 8]], (viii) [3, 8]
['nested', 'lists']] (ix) A
(iii) 5 (x) ['A', [5, 3, 8]]
(iv) [5, 3, 8] (xi) ['A', [5, 3, 8]]
(v) []
(xii) 45
(vi) [5, 3, 8]
66

59
XI
Output Questions
Find the output of the following code :

© CS-DEPT DPS MATHURA ROAD


mylist=[10,20,30,40]
mylist[0]=0
for i in range(len(mylist)):
mylist[i]*=2
mylist[3]+=100
print(mylist)

60
XI
Solutions

© CS-DEPT DPS MATHURA ROAD


[0, 40, 60, 180]

61
XI
Output Questions
Find the output of the following code :

© CS-DEPT DPS MATHURA ROAD


names1 = ['Shreya', 'Mohak', 'Mahesh', 'Dwivedi']
names2 = names1
names3 = names1[:]
names2[0] = 'Vishal'
names3[1] = 'Jay'
sum = 0
for ls in (names1,names2,names3):
if ls[0] == 'Vishal':
sum += 2
if ls[1] == 'Jay':
sum += 5
print (sum)

62
XI
© CS-DEPT DPS MATHURA ROAD
63
2

9
Solutions

XI
Output Questions
Find the output of the following code :

© CS-DEPT DPS MATHURA ROAD


Data=["P",20,"R",10,"S",30] Times Alpha Add
Times=0 1 P$ 20
4 P$R$ 30
Alpha=""
Add=0 9 P$R$S$ 60
for c in range(1,6,2):
Times=Times+c
Alpha=Alpha+Data[c-1]+"$"
Add=Add+Data[c]
print(Times,Alpha,Add)

64
XI
Solutions

© CS-DEPT DPS MATHURA ROAD


1 P$ 20
4 P$R$ 30
9 P$R$S$ 60

65
XI
Output Questions
Find the output of the following code :

© CS-DEPT DPS MATHURA ROAD


arr = [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
arr[i - 1] = arr[i] i arr[i-1]
1 2
2 3
for i in range(0, 6): 3 4
print(arr[i], end = " ") 4 5
5 6

66
XI
© CS-DEPT DPS MATHURA ROAD
67
234566
Solutions

XI
Output Questions
Find the output of the following code :

© CS-DEPT DPS MATHURA ROAD


L=[] L1=[10,20,30,20,10]
for i in range(4): L2=[]
L.append(2*i+1) for i in L1:
print(L[::-1]) if i not in L2:
L2.append(i)
print(L1,L2,sep="&" )

68
XI
Solutions

© CS-DEPT DPS MATHURA ROAD


[7,5,3,1] [10,20,30,20,10] & [10,20,30]

69
XI
Practice Questions
The correct syntax for arranging the items of the list L in descending order is:

© CS-DEPT DPS MATHURA ROAD


a. L.sort(reverse = True)
b. L.sort(reverse = False)
c. L.sort(L,reverse = True)
d. L.sort(reverse)
Write single line statements for each of the following:
a. To declare a list named SUBJECTS, which stores the names of five subjects
"English","Maths","Physics","Chemistry", "Computer Science".
b. To arrange the above list SUBJECTS in ascending order.
c. To add one more subject, "Physical Education" in the list SUBJECTS
d. To remove the subject "Maths" from the list SUBJECTS
e. To display the contents of list SUBJECTS in reverse order.
70
XI
Practice Programs
Q1. Write a program to input two different lists and then create a list that all the common elements
from the two lists. For example :

© CS-DEPT DPS MATHURA ROAD


If list1=[11, 22, 44, 55, 99, 66] and list2=[44, 22, 55, 12, 56] then

list3 is to be created as list3 = [22, 44, 55]

Q2. Write a program to input a list and then double the even elements of the list and triple the odd
elements of the list.
Q3. Write a program to input a list containing names of cities and then display the names of all those
cities that start with the alphabet ‘A’.
For example if the list contains [“AHMEDABAD”, CHENNAI”, “NEW DELHI”, “AMRITSAR”,“ AGRA”],
then the program should print AHMEDABAD, AMRITSAR and AGRA.

Q4. Write a program to enter a list and then push all the zeros in the list to the end of the list.

For example: If the list contains [0, 2, 3, 4, 6, 7, 0, 1], then the program should re-arrange the
elements as [2, 3, 4, 6, 7,1, 0, 0]

71
XI

You might also like