Python Code

16 Python Built in Functions for the Lists with Basic Examples

There are many Python Built in Functions that faster the development process. It will be very useful for you to know these built in Functions. Let’s know all these functions.

Python Built in Functions for the Lists

1.append()

It will add a single item to the end of the list.

#create a list
list = ["Data Science Learner",10,40.6]
print("List before append()method")
print(list)
#Add Single Element to The List
list.append(20)
#print the list
print("List after append()method")
print(list)

Output

List before append()method
['Data Science Learner', 10, 40.6]
List after append()method
['Data Science Learner', 10, 40.6, 20]

2. extend()

This method will add all the elements from one list to another list to the end

#create a list
list = ["Data Science Learner",10,40.6]
list2 =["Big Data",30,"Machine Learning",20]
print("Lists before extend()method")
print(list)
print(list2)
#Add list Elements to The end of the List
list.extend(list2)
#print the list
print("List after extend()method")
print(list)

Output

Lists before extend()method
['Data Science Learner', 10, 40.6]
['Big Data', 30, 'Machine Learning', 20]
List after extend()method
['Data Science Learner', 10, 40.6, 'Big Data', 30, 'Machine Learning', 20]

3. insert()

It will insert an element at the specified index location.

#create a list
list = ["Data Science Learner",10,40.6]
print("List before insert()method")
print(list)
#Add list Elements to The given index of the List
list.insert(1,"Machine Learning")
#print the list
print("List after insert()method")
print(list)

Output

List before insert()method
['Data Science Learner', 10, 40.6]
List after insert()method
['Data Science Learner', 'Machine Learning', 10, 40.6]

4. remove()

This method willl search for the specific element in the list and remove it.

#create a list
list = ["Data Science Learner",10,40.6,"Machine Learning"]
print("List before remove()method")
print(list)
#remove the machine element from the list
list.remove("Machine Learning")
#print the list
print("List after remove()method")
print(list)

Output

List before remove()method
['Data Science Learner', 10, 40.6, 'Machine Learning']
List after remove()method
['Data Science Learner', 10, 40.6]

5. count()

It will count the occurences of the a particlular element. It means how many time a number is inside the list.

#create a list
list = ["Data Science Learner",10,40.6,20,10,50,10]
print("List before count()method")
print(list)
#print the list
print("count of the element")
print(list.count(10)) #count the occurences of the element 10
print(list.count(20)) ##count the occurences of the element 20

Output

List before count()method
['Data Science Learner', 10, 40.6, 20, 10, 50, 10]
count of the element
3
1

6. index()

On using this method , it will return the first location or  index of the searchable element.

#create a list
list = ["Data Science Learner",10,40.6,20,10,50,10]
print("List before index()method")
print(list)
#print the list
print("Location of the element is")
print(list.index(10))# index of the element

Output

List before index()method
['Data Science Learner', 10, 40.6, 20, 10, 50, 10]
Location of the element is
1

7. pop()

It removes a particlular element from the list at a specificed location.

#create a list
list = ["Data Science Learner",10,40.6,20,10,50,10]
print("List before pop()method")
print(list)
#pop the element
list.pop(5) #pop the 50 element
#print the list
print("List after the pop() method")
print(list)

Output

List before pop()method
['Data Science Learner', 10, 40.6, 20, 10, 50, 10]
List after the pop() method
['Data Science Learner', 10, 40.6, 20, 10, 10]

8. reverse()

If you want to reverse the elements of the list, then use this method.

#create a list
list = ["Data Science Learner",10,40.6,20,10,50,10]
print("List before reverse()method")
print(list)
#reverse the element
list.reverse()
#print the list
print("List after the reverse() method")
print(list)

Output

List before reverse()method
['Data Science Learner', 10, 40.6, 20, 10, 50, 10]
List after the reverse() method
[10, 50, 10, 20, 40.6, 10, 'Data Science Learner']

9. sort()

This method will sort the elements in the ascending or descending order. Make sure that all the elements in the list must be of the same datatype like string or integer. Otherwise, it will give an error.

#create a list
list = [10,40.6,20,10,50,10]
print("List before reverse()method")
print(list)
#sort the element
list.sort()
#print the list
print("List after the sort() method")
print(list)

Output

List before reverse()method
[10, 40.6, 20, 10, 50, 10]
List after the sort() method
[10, 10, 10, 20, 40.6, 50]

10. copy()

It will make an exact copy of the list. You can also copy the list like old_list = new_list. But the new list will not be the copy of the old list if old_list  is modified. Therefore we use copy() method for first copying the old list and then you can modify the new list.

#create a list
list = ["Machine learning",10,40.6,20,10,50,10]
print("List before copy()method")
print(list)
#copy the List
newList = list.copy()
#print the list
print("List after the copy() method")
print(newList)

Output

List before copy()method
['Machine learning', 10, 40.6, 20, 10, 50, 10]
List after the copy() method
['Machine learning', 10, 40.6, 20, 10, 50, 10]

11. clear()

It clears all the items from the list.

#create a list
list = ["Machine learning",10,40.6,20,10,50,10]
print("List before clear() method")
print(list)
#clear the List
list.clear()
#print the list
print("List after the clear() method")
print(list)

Output

List before clear() method
['Machine learning', 10, 40.6, 20, 10, 50, 10]
List after the clear() method
[]

12. any()

It will return the true or false value if a list is iterable or not.

#create a list
list = ["Machine learning",10,40.6,20,10,50,10]
list2 = ["Machine learning"]
list3 = []
print("List before any() method")
print(list)
print(list2)
#print the True or False
print("Value after the any() method")
print(any(list)) #list
print(any(list2)) #list2
print(any(list3)) #list3

Output

List before any() method
['Machine learning', 10, 40.6, 20, 10, 50, 10]
['Machine learning']
Value after the any() method
True
True
False

13. filter()

It will construct an iterator from items in the iterable lists. filter() has two parameters function and iterable list. It only returns all the filter values

The syntax is

filter(function_name,list)
#create a list
list = ["Machine learning",10,40.6,20,10,50,10]
print("List before filter() method")
print(list)
#filter list values
def filterList(list):
  listFilter = [10,20,50]
   if(list in listFilter):
    return True
   else:
    return False
#filter
finalFilterList = filter(filterList,list) # filter the list
print("List after filter")
for f in finalFilterList: #iterate the filtered list
print(f)

Output

List before filter() method
['Machine learning', 10, 40.6, 20, 10, 50, 10]
List after filter
10
20
10
50
10

14. list()

This method will convert the strings, tuples, sets, dictionary into a list.

#create a list
set = {"Machine learning",10,40.6,20,10,50,10} #set
dis = {"key1":"value1","key2":"value2","key3":"value3"} #dictionary
string = "Data Science Learner" #string

#convert to list
print(list(set))
print(list(dis))
print(list(string))

Output

['Machine learning', 40.6, 10, 50, 20]
['key1', 'key2', 'key3']
['D', 'a', 't', 'a', ' ', 'S', 'c', 'i', 'e', 'n', 'c', 'e', ' ', 'L', 'e', 'a', 'r', 'n', 'e', 'r']

15. len()

It will return the total number of items in the list.

#create a list
list = ["Machine learning",10,40.6,20,10,50,10] #list
print("List before len() method")
print(list)
#lenght of list
print(len(list))

Output

List before len() method
['Machine learning', 10, 40.6, 20, 10, 50, 10]
7

16. map()

This method will apply the function to the  each item of the list. Its syntax is given below.

map(function_name, list1,list2,...)
#create a list
list1 = [1,2,3,4,5,6] #list
print("List before len() method")
print(list1)
#define the function
  def cubeNumber(number):
    return number*number*number
#call the map method
result = map(cubeNumber,list1)
print(list(result))# convert the result into list

Output

List before len() method
[1, 2, 3, 4, 5, 6]
[1, 8, 27, 64, 125, 216]

 

 

 

 

 

 

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner