0% found this document useful (0 votes)
3 views

List Functions

The document provides a comprehensive overview of various list functions and methods in Python, including len(), list(), index(), append(), extend(), insert(), pop(), remove(), clear(), count(), reverse(), and sort(). Each function is explained with examples demonstrating its usage. Additionally, it includes a set of questions and answers related to list operations to reinforce understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

List Functions

The document provides a comprehensive overview of various list functions and methods in Python, including len(), list(), index(), append(), extend(), insert(), pop(), remove(), clear(), count(), reverse(), and sort(). Each function is explained with examples demonstrating its usage. Additionally, it includes a set of questions and answers related to list operations to reinforce understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Class XIIA CS

List Functions/Methods

The List functions are:

1) len()
This function returns the length of the list. The length of the list is equal to the
number of elements present in the list.
>>> l = ['Mon','Tue','Wed','Thurs','Fri','Sat']
>>> print(len(l))
6
>>> l=[['Mon','Tue','Wed'],['Thu','fri'],'Sat','Sun']
>>> print(len(l))
4

2) list()
This function will convert the passed parameters into list.
>>> L = list("XIICS2024-25")
>>> L
[‘X’,’I’,’I’,’C’,’S’,’2’,’0’,’2’,’4’,’-‘,’2’,’5’]

3) index()
This method is used to find first index position of a value in a list. It returns-1 if value
is not found in the list.
>>> l=[11,22,27,34,67,98]
>>> l.index(27)
2
>>> L1=[13,18,11,16,18,14]
>>> print(L1.index(18))
1

4) append() :
This method is used to add a new element at the end of list.
>>> L1=[13,18,11,16,18,14]
>>> L1.append(100)
>>> print(L1)
[13, 18, 11, 16, 18, 14, 100]
>>> L1=[13,18,11,16,18,14]
>>> L1. Append([12,14])
>>> print(L1)
[1,3,5,10,[12,14]]
5) extend()
It is used to append elements of another list at the end of existing list.
>>>L1=[‘a’,’b’,’c’,]
>>>L2=[‘d’,’e’]
>>>L1.extend(t2)
>>> print(L1)
[‘a’,’b’,’c’,’d’,’e’]

6) insert()
The insert () method is used to insert an element at any position in a list.
>>> L1=[13,18,11,16,18,14]
>>> L1.insert(1,100) #insert 100 at index 1 i.e. second element.
>>> print(L1)
[13, 100, 18, 11, 16, 18, 14]
>>>L1=[13,18,11,16,18,14]
>>>L1.insert(-2,100)
[13, 18, 11, 16, 100, 18, 14]

7) pop()
It is used to remove and return an element from the given index in the list. If no
index is specified, pop() removes and returns the last item in the list.
>>> L1=[13,18,11,16,18,14]
>>> val=L1.pop() #Deletes last element of list and stores in val.
>>> print(val)
14
>>> L1=[13,18,11,16,10,14]
>>> val=L1.pop(1) #element at index 1 i.e. 18 is deleted and stored in val
>>> print(val)
18

8) remove()
It removes the first occurrence of an item from the list.
>>> L1=[13,18,11,16,18,14]
>>> L1.remove(11)
>>> print(L1) #List will not contain 11
[13, 18, 16, 18, 14]

9) clear()
This method is used to remove all the items from a list and make the list empty. It
doesn’t delete the list permanently.
>>> L1=[13,18,11,16,18,14]
>>> print(L1.clear())
[]
10) count()
This function is used to count and return number of times a value exists in a list. If
the given value is not in the list, it returns zero.
>>> L1=[13,18,11,16,18,14]
>>> L1.count(18) #18 appears twice in list L1.
2

11) reverse()
It is used to reverse the items of the list.
>>> L1=[13,18,11,16,18,14]
>>> L1.reverse()
>>> print(L1)
[14, 18, 16, 11, 18, 13]

12) sort()
This function arranges the items of the list in increasing order. We can arrange
elements in decreasing order by using function as sort (reverse=True).
>>> L1=[13,18,11,16,18,14]
>>> L1.sort()
>>> print(L1)
[11, 13, 14, 16, 18, 18]

>>> L1=[13,18,11,16,18,14]
>>> L1.sort(reverse=True)
>>> print(L1)
[18, 18, 16, 14, 13, 11]

Some important Questions and answers:

Q.1 Identify the valid declaration of L:


L = [‘Mon’, ‘23’, ‘hello’, ’60.5’]
Ans: List

Q.2 Given the lists L=[1,3,6,82,5,7,11,92] , write the output of print(L[2:5])


Ans: [6,82,5]

Q.3 Write the Python statement to insert an element 200 at the third position, in the
list L1.
Ans: L1.insert(2,200)
Q.4 Identify the output of the following Python statements.
x = [[10.0, 11.0, 12.0],[13.0, 14.0, 15.0]]
y = x[1][2]
print(y)
Ans: 15.0

Q.5 Identify the output of the following Python statements.


lst1 = [10, 15, 20, 25, 30]
lst1.insert( 3, 4)
lst1.insert( 2, 3)
print (lst1[-5])
Ans: 3

Q,6 A list named studentAge stores age of students of a class. Write the Python
command to import the required module and (using built-in function) to display the
most common age value from the given list.
Ans:
import statistics
print( statistics.mode(studentAge) )

Q.7 Predict the output of the following code:

Ans:
4*L
33*4
21*S
10*6

******* Prepared by Vijay Chawla, PGT(CS), PM SHRI KV Pitampura, Delhi *******

You might also like