python offers several built-in functions to perform various operations on lists.
append(): Used for appending and adding elements to List.It is used to add elements to the last position of List.
Syntax:
list.append (element)
Example:
List
=
['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)
output:
['Mathematics', 'chemistry', 1997, 2000, 20544]
insert(): Inserts an elements at specified position.
Syntax:
list.insert(<position, element)
Note: Position mentioned should be within the range of List, as in this case between 0 and 4, elsewise would throw IndexError.
List
=
['Mathematics', 'chemistry', 1997, 2000]
List.insert(2,10087)
print(List)
output:
['Mathematics', 'chemistry', 10087, 1997, 2000, 20544]
extend(): Adds contents to List 2 to the end of List 1.
Syntax:
List 1.extend(List 2)
Example:
List 1 =
[1, 2, 3]
List 2 =
[2, 3, 4, 5]
# Add List 2 to List 1
List 1.extend(List 2)
print(List 1)
#Add List 1 to List 2 now
List 2.extend(List 1)
print(List 2)
Output:
[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]
index(): Returns the index of first occurrence. Start and End index are not necessary parameters.
Syntax:
List.index(element[,start[,end]])
Example:
List
=
[1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2))
Output:
1
remove(): Element to be deleted is mentioned using list name and element.
Syntax:
list.remove(element)
Example:
List
=
[2.3, 4.445, 3, 5.33, 1.054, 2.5]
List.remove(3)
print(List)
output:
[2.3, 4.445, 5.33, 1.054, 2.5]
reverse(): Sort the given data structure (both tuple and list) in ascending order. Key and reverse_flag are not necessary parameter and reverse_flag is set to False, if nothing is passed through sorted().
Syntax:
sorted([list[,key[,Reverse_Flag]]])
list.sort([key,[Reverse_flag]])
Example:
List
=
[2.3, 4.445, 3, 5.33, 1.054, 2.5]
#Reverse flag is set True
List.sort(reverse=True)
#List.sort().reverse(), reverses the sorted list
print(List)
Output:
[5.33, 4.445, 3, 2.5, 2.3, 1.054]
length:Calculates total length of List.
Syntax:
len(list_name)
Example:
List
=
[1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(len(List))
output:
10
The sort function can be used to sort a list in ascending, descending or user defined order.
To sort the list in ascending order.
List_name.sort()
This will sort the given list in ascending order.
This function can be used to sort list of integers, floating point number, string and others.
Example:
numbers =
[1, 3, 4, 2]
# Sorting list of Integers in ascending
numbers.sort()
print(numbers)
Output:
[1, 2, 3, 4]
To sort the list in descending order.
list_name.sort(reverse=True)
This will sort the given list in descending order.
Example:
numbers =
[1, 3, 4, 2]
# Sorting list of Integers in descending
numbers.sort(reverse =
True)
print(numbers)
Output:
[4, 3, 2, 1]
The clear() method removes all elements from the set.
Syntax:
set.clear()
parameters:
The clear() method doesn't take any parameters.
Return:
The clear() method doesn't return any value.
Example:
# set of letters
GEEK =
{'g', 'e', 'e', 'k', 's'}
print('GEEK before clear:', GEEK)
# clearing vowels
GEEK.clear()
print('GEEK after clear:', GEEK)
Output:
GEEK before clear: set(['s', 'e', 'k', 'g'])
GEEK after clear: set([])
count() function in an inbuilt function in python programming language that returns the number of occurrences of a substring in the given string.
Syntax:
string.count(substring, start=…, end=…)
Parameters:
The count() function has one compulsory and two optional parameters.
Mandatory paramater:
1)substring – string whose count is to be found.
Optional Parameters:
1)start (Optional) – starting index within the string where search starts.
2)end (Optional) – ending index within the string where search ends.
Return Value:
count() method returns an integer that denotes number of times a substring occurs in a given string.
Below is the Python implementation of the count() method without optional parameters:
Example:
string =
"geeks for geeks"
print(string.count("geeks"))
output:
2