In-built function in List

clear ()

The clear () function used to remove all the elements in list.  After remove the elements it does not display anything.
Syntax:
clear ( )

Example:  

list = ['a','b','c','d']  
for l in list:  # Iterating list  
    print(l)  
list.clear()  
print("After clearing:")  
for l in list:  # Iterating list  
    print(l) 

Output:

a
b
c
d
After clearing:
>>> 

copy ()

The copy () function used to copy the list and return the copied list. This function does not contained any parameter.
Syntax:
copy()

Example:

list1 = [6,8,2,4] # int list  
copylist = []  
# Calling Method  
copylist = list1.copy()  
# Displaying result  
print("Original list:",list1)  
print("Copy list:",copylist) 

Output:

Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]
>>> 

count ()

The count () function used to count the number of elements present in the list.
Syntax:
count(value)

Example:

list = [1,2,2,3,4,5,2]  
# Method calling  
count = list.count(2)  
# Displaying result  
print("count of 2 :",count)

Output:

count of 2 : 3
>>> 

extend ()

The extend () function used to extend the list after appending the elements from the iterable.
Syntax:
extend(iterable)

Example:

list1=[1,2,3,4,5]
list2=[6,7,8,9,10]
print("Before extend")
for i in list1:
    print(i)
print("After extend")
list1.extend(list2)
for i in list1:
    print(i)

Output:

Before extend
1
2
3
4
5
After extend
1
2
3
4
5
6
7
8
9
10
>>> 

index ()

The index () method in python return the index number of the element which are present in the list.
If the index is not found then the error will be came.
Syntax:
index (x[start,[end]])


Example:

g = ['g','r','a','p','e','s']  
# Method calling  
i=g.index('r')
i2=g.index('a',0,3)
# Displaying result  
print("Index of r :",i)
print("Index of a :",i2)

Output:

Index of r : 1
Index of a : 2
>>> 

insert ()

The insert () method used to insert elements at the particular index in the list.
Syntax:
insert (i , x)
Where, i is index number and x is element to be inserted.

Example:

list = [1,2,3]  
for l in list:  #Iterating list  
    print(l)  
list.insert(3,[4,5,6])  
print("After extending:")  
for l in list:  #Iterating list  
    print(l)

Output:

1
2
3
After extending:
1
2
3
[4, 5, 6]
>>> 

pop ()

The pop() method used to pop out the elements at the specified index.This method return the popped elements.
Syntax:
pop([i])
Where i is a index number.

Example:

list = [1,2,3,4,5,6,7,8]  
for l in list:  # Iterating list  
    print(l)  
list.pop(2)  
print("After poping from 2nd index:")  
for l in list:  # Iterating list  
    print(l)
list.pop(-3)
print("After poping from -3 index:")
for l in list:
    print(l)

Output:

1
2
3
4
5
6
7
8
After poping from 2nd index:
1
2
4
5
6
7
8
After poping from -3 index:
1
2
4
5
7
8
>>> 

sort ()

The sort () function used to sort the elements of the list in ascending or descending order. It takes 'reverse' as a parameter to sort the element in descending order.
Syntax:
sort ()

Example:

a=['p', 'y', 't', 'h', 'o', 'n'] # Char list  
e=[6,8,2,4] # int list
f=[1,2,4,3]
print(a)  
print(e)
print(f)
# Calling Method  
a.sort()  
e.sort()
f.sort(reverse=True)     # sort in reverse order  
# Displaying result  
print("\nAfter Sorting:\n",a)  
print(e)
print(f)

Output:

['p', 'y', 't', 'h', 'o', 'n']
[6, 8, 2, 4]
[1, 2, 4, 3]

After Sorting:
 ['h', 'n', 'o', 'p', 't', 'y']
[2, 4, 6, 8]
[4, 3, 2, 1]
>>> 

Comments

Popular Posts