BUILT IN FUNCTION OF LIST IN PYTHON,APPEND,INSERT,EXTEND,REMOVE,POP,DEL,MAX,MIN,REVERSE
VIDEO LINK:
https://computerassignmentsforu.blogspot.com/p/listinpythonpart3.html
2. What is Built-in functions?
Built-in functions that predefined and supplied along with the compiler are
known as built-in functions. Inbuilt are those function which
already in the library and we did not need to create our own.
Let us discuss the built in functions of list
4. Append It adds a single item to the end of the list
list_name.append( item or value)Syntax:
Example:
if we declare and initialize the list with numbers as shown below:
10 20 30 40
no=[10,20,30,40]
no.append(50)
print(no)
------------output-----------
10 20 30 40 50
0 1 2 3
0 1 2 3 4
The new element added at the end (Right side)
50
4
5. Insert
It can be used to insert an element or object at a specified index.
This function takes two arguments: the index where an item or
object is to be inserted and the item or element itself.
list_name.insert(index_no , value)
Syntax:
index_no : is the index where the new value is to be inserted
value: is the new value to be inserted in the list
6. Insert
Example:
If there are list in a python program no and numbers are 10, 20, 30 and
we want to new number 25 between 20 and 30 in a list no.
no= [10, 20, 30]
no.insert (2,25)
print(no)
----------output----------
0 1 2
10 20 30
10 20 25 30
0 1 2 3
0 1
10 20
2
25
2
30
3
7. Insert Example2:
If there are list in a python program names , names in a list are “Vinay”,
“Sonia”, “Deepak” and we want to new name “Kim” before “Vinay” in a
list name
names= [“Vinay”,”Sonia”,”Depak”]
names.insert(0,”Kim”)
print(names)
----------output----------
‘Kim’ ‘Vinay’ ‘Sonia’ ‘Deepak’
0 1 2 3
‘Vinay’ ‘Sonia’ ‘Deepak’
0 1 2
‘Vinay’ ‘Sonia’ ‘Deepak’‘Kim’
1 2 30
8. Extend
adds one list at the end of another list
Syntax: list_name.extend(list2_name)
list_name: It is the primary list, which will be extended
list2_name: It is the list which will added to list_name
Example: no1= [10, 20, 30]
no2= [15, 25, 35]
no1.extend (no2)
print (no1)
---------output---------- 10 20 30 15 25 35
10 20 30
15 25 35
10 20 30 15 25 35
Always towards the right side only
In this case no1 extended and concatenate list no2 with no1
to add to something in order to make it bigger or longer
9. So What is the difference between append() and extend()?
For Example: If we want to add 70,80,90 numbers in a list no.
no= [10, 20, 30]
0 1 2
10 20 30
Using : Append() method.
no.append(70) 0 1 2
10 20 30
3
70
no.append(80)
no.append(90)
0 1 2 3
10 20 30 70
4
80
0 1 2 3 4
10 20 30 70 80
5
90
print(no)
----------output---------- [10, 20, 30, 70, 80, 90]
10. So What is the difference between append() and extend()?
For Example: If we want to add 70,80,90 numbers in a list no.
no= [10, 20, 30]
0 1 2
10 20 30
Using : extend() method.
no.extend( ) 0 1 2
10 20 30
3 4 5
70 80 90
print(no)
----------output----------
[10, 20, 30, 70, 80, 90]
[70,80,90]
11. Index The index() function or method returns the index of first
matched item from the list. It returns the first occurrence
of an item for which the index position is to be searched
for in the list.
Syntax: list_name.index(item)
Example:
no= [10, 20, 30,40,50,60]
print (no.index(40))
---------output----------
3
10 20 30 40 50 60
0 1 2 3 4 5
13. Index
Example:
days=[“MON”, “TUE”, “WED”, “THU”, “FRI”, “SAT”, “SUN”]
print(days.index(‘TUE’))
---------output----------
1
MON TUE WED THU FRI SAT SUN
0 1 2 3 4 5 6
14. Sort This function sort() the items of the list, by default in
ascending or descending order.
Syntax: list_name.sort()
Example:
no= [40,10,50,20,60,30]
no.sort()
---------output----------
40 10 50 20 60 30
0 1 2 3 4 5
10 20 30 40 50 60
0 1 2 3 4 5print (no)
15. clear
Example:
days=[“MON”, “TUE”, “WED”, “THU”, “FRI”, “SAT”, “SUN”]
days.clear()
---------output----------
It is Empty list nothing display
MON TUE WED THU FRI SAT SUN
0 1 2 3 4 5 6
The clear() method removes all items from the list.
18. Deletion operation
Python provide operators for deleting or removing an item from a list.
There are three method for deleting items from a list:-
If index is known, we can use pop() or del() method.
If the element is known, not the index value remove() method can be
used. It is used to delete items from the list using item or value
inside the list.
To remove more than one element, del() method is used with the
help of slice.
pop() del() remove()
19. pop() It removes the element from the specified index, and also
returns the element which was removed.
Syntax: list_name.pop()
Example:
no= [1,2,4,5,6,8,9,10,12,15,18]
0 1 2 3 4 5 6 7 8 9 10
print(no.pop(4))
---------output----------
6
print(no) [1,2,4,5,8,9,10,12,15,18]
Here the element deleted will be returned to print to display
20. pop() We can also store the deleted item inside another variable
also.
Syntax: list_name.pop()
Example:
no= [1,2,4,5,6,8,9,10,12,15,18]
0 1 2 3 4 5 6 7 8 9 10
k=no.pop(2)
---------output----------
4
print(k)
Here the element deleted will be returned to variable k
21. del() It removes the element from the specified index or using slicing ,
but does not returns the element which was removed.
Syntax: del listname[indexno]
Example:
no= [1,2,4,5,6,8,9,10,12,15,18]
0 1 2 3 4 5 6 7 8 9 10
del no[4]
---------output----------print(no)
[1,2,4,5,8,9,10,12,15,18]
del listname[start:end]OR
Index number Use of slicing method
22. del() It removes the element from the list using slicing.
For Example: If we delete values from index no. 4 to 8.
Syntax:
Example:
no= [1,2,4,5,6,8,9,10,12,15,18]
0 1 2 3 4 5 6 7 8 9 10
del no[ : ]
---------output----------print(no)
[1,2,4,5,15,18]
del listname[start:end]
Use of slicing method
4 9 9 means end value -1
23. remove() This function is used when we know the element number
not the index value.
Syntax: Listname.remove(item or value)
Example:
no= [1,2,4,5,6,8,9,10,12,15,18]
0 1 2 3 4 5 6 7 8 9 10
no.remove(12)
---------output----------
print(no)
[1,2,4,5,8,9,10,15,18]
It will delete the item 12 from the list no
24. max() It returns the element value with the maximum value
from the list.
Example:
no= [41,12,40,25,36,18,39,10,42,35,28]
0 1 2 3 4 5 6 7 8 9 10
print(max(no))
---------output----------42
print(min(no))
min() It returns the element value with the minimum value
from the list.
10
25. reverse() It reverse the order of elements in a list.
Example:
no= [15,16,17,18,19]
0 1 2 3 4
no.reverse()
---------output----------print(no)
[19,18,17,16,15]