Page 219 - Robotics and AI class 10
P. 219
The extend() Function
The extend() function is used to append multiple values at a time in a list. The syntax of the extend() function is:
list.extend(iterable value)
where iterable value can be a list. Examples are:
Commands Output
n=[1,2,3,4] [1, 2, 3, 4, 5]
m=[5]
n.extend(m)
print(n)
pets = ['cat', 'dog', 'rabbit'] ['cat', 'dog', 'rabbit', 'tiger', 'lion']
wild=['tiger', 'lion']
pets.extend(wild)
print(pets)
Some other Functions Used with List
There are some other functions also available to use with the list which are as follows:
• sort(): This function sorts the list in ascending or descending order. This is done "in the list itself" and works for
the list with values of the same data types. For example,
city=["Delhi", "Mumbai", "Kolkata", "Chennai"]
city.sort() #sorts the list by default in ascending order
print(city)
Output: ["Chennai", "Delhi", "Kolkata", "Mumbai"]
city.sort(reverse=True)#sorts the list descending order
Output: ["Mumbai", "Kolkata", "Delhi", "Chennai"]
Searching a value in a list
Example 1
Searching a specific value and displaying its number of occurrences in a list.
A=[10,23,45,10,12,45,10,56]
s=int(input("enter value you want to search :"))
count=0
for i in A:
if i==s:
count+=1
print(s," occurs ",count," times")
Example 2
Input a value and search whether it exists in a list or not. If it exists then display its index number.
flowers=["Rose","Lily","Lotus","Sunflower"]
s=input("Input a name of a flower :")
Introduction to Data and Programming with Python 217

