PYTHON LIST BUILT-IN FUNCTIONS
INTRODUCTIONS
Python provides some useful built-in functions which can be used while working with lists.
len() FUNCTION
len() function returns the no of elements present in a list including duplicates.
# Commonly used Python list built-in methods example 1
int_list = [400, 200, 600, 100, 150, 500, 600]
print("\nElement of integer list ->", int_list)
print("\nNo of Elements in integer list ->", len(int_list))
str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP']
print("\nElement of string list ->", str_list)
print("\nNo of Elements in string list ->", len(str_list))
mix_list = [200, 600, 100, 'Python', 'Java', 5.8, True]
print("\nElement of mixed list ->", mix_list)
print("\nNo of Elements in mixed list ->", len(mix_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, 600]
No of Elements in integer list -> 7
Element of string list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP']
No of Elements in string list -> 7
Element of mixed list -> [200, 600, 100, 'Python', 'Java', 5.8, True]
No of Elements in mixed list -> 7
count() FUNCTION
count() function returns the number of occurrences of an element in a given list.
# Commonly used Python list built-in methods example 2
int_list = [400, 200, 600, 100, 150, 500, 600]
print("\nElement of integer list ->", int_list)
print("\nNo of occurrences of 600 in integer list ->", int_list.count(600))
str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP']
print("\nElement of string list ->", str_list)
print("\nNo of occurrences of PHP in string list ->", str_list.count('PHP'))
mix_list = [200, 600, 100, 5.8, 'Python', 'Java', 5.8, True]
print("\nElement of mixed list ->", mix_list)
print("\nNo of occurrences of True in mixed list ->", mix_list.count(True))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, 600]
No of occurrences of 600 in integer list -> 2
Element of string list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'PHP']
No of occurrences of PHP in string list -> 2
Element of mixed list -> [200, 600, 100, 5.8, 'Python', 'Java', 5.8, True]
No of occurrences of True in mixed list -> 1
sum() FUNCTION
sum() function returns the total of all the elements of a list having integer or floating number or both.
# Commonly used Python list built-in methods example 3
int_list = [400, 200, 600, 100, 150, 500, 600]
print("\nElement of integer list ->", int_list)
print("\nTotal of all the elements in integer list ->", sum(int_list))
float_list = [4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8]
print("\nElement of floating list ->", float_list)
print("\nTotal of all the elements in floating list ->", sum(float_list))
mixed_list = [4.5, 2.6, 600, 10.3, 150, 20.5, 600]
print("\nElement of mixed list ->", mixed_list)
print("\nTotal of all the elements in mixed list ->", sum(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, 600]
Total of all the elements in integer list -> 2550
Element of floating list -> [4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8]
Total of all the elements in floating list -> 77.3
Element of mixed list -> [4.5, 2.6, 600, 10.3, 150, 20.5, 600]
Total of all the elements in mixed list -> 1387.9
max() FUNCTION
max() function returns the maximum of all the elements of a list having integer or floating number or both.
# Commonly used Python list built-in methods example 4
int_list = [400, 200, 600, 100, 150, 500, 600]
print("\nElement of integer list ->", int_list)
print("\nHighest element in integer list ->", max(int_list))
float_list = [4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8]
print("\nElement of floating list ->", float_list)
print("\nHighest element in floating list ->", max(float_list))
mixed_list = [4.5, 2.6, 60, 10.3, 150, 20.5, 60]
print("\nElement of mixed list ->", mixed_list)
print("\nHighest element in mixed list ->", max(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, 600]
Highest element in integer list -> 600
Element of floating list -> [4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8]
Highest element in floating list -> 20.5
Element of mixed list -> [4.5, 2.6, 60, 10.3, 150, 20.5, 60]
Highest element in mixed list -> 150
min() FUNCTION
min() function returns the minimum of all the elements of a list having integer or floating number or both.
# Commonly used Python list built-in methods example 5
int_list = [400, 200, 600, 100, 150, 500, 600]
print("\nElement of integer list ->", int_list)
print("\nLowest element in integer list ->", min(int_list))
float_list = [4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8]
print("\nElement of floating list ->", float_list)
print("\nLowest element in floating list ->", min(float_list))
mixed_list = [4.5, 2.6, 60, 10.3, 150, 20.5, 60]
print("\nElement of mixed list ->", mixed_list)
print("\nLowest element in mixed list ->", min(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, 600]
Lowest element in integer list -> 100
Element of floating list -> [4.5, 2.6, 6.9, 10.3, 15.7, 20.5, 16.8]
Lowest element in floating list -> 2.6
Element of mixed list -> [4.5, 2.6, 60, 10.3, 150, 20.5, 60]
Lowest element in mixed list -> 2.6
all() FUNCTION
all() function returns True if all the elements are True. Element 0 is considered as False. But any negative value is considered as True.
# Commonly used Python list built-in methods example 6
int_list = [400, 200, 600, 100, 150, 500, -60]
print("\nElement of integer list ->", int_list)
print("\nAll the elements in integer list are True ? ->", all(int_list))
str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
print("\nElement of string list ->", str_list)
print("\nAll the elements in string list are True ? ->", all(str_list))
mixed_list = [45, -2.6, 'Python', 'Java', 'C++', 20.5, 60]
print("\nElement of mixed list ->", mixed_list)
print("\nAll the elements in mixed list are True ? ->", all(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, -60]
All the elements in integer list are True ? -> True
Element of string list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
All the elements in string list are True ? -> True
Element of mixed list -> [45, -2.6, 'Python', 'Java', 'C++', 20.5, 60]
All the elements in mixed list are True ? -> True
In the above example, we have used both positive and negative values. All the cases python has returned True.
# Commonly used Python list built-in methods example 7
int_list = [400, 200, 600, 100, 150, 500, -60, 0]
print("\nElement of integer list ->", int_list)
print("\nAll the elements in integer list are True ? ->", all(int_list))
str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0]
print("\nElement of string list ->", str_list)
print("\nAll the elements in string list are True ? ->", all(str_list))
mixed_list = [45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0]
print("\nElement of mixed list ->", mixed_list)
print("\nAll the elements in mixed list are True ? ->", all(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, -60, 0]
All the elements in integer list are True ? -> False
Element of string list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0]
All the elements in string list are True ? -> False
Element of mixed list -> [45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0]
All the elements in mixed list are True ? -> False
In the above example, all the lists are having 0 as an element. That’s why python has returned False in all the cases.
any() FUNCTION
any() function returns True if any of the elements is True.
# Commonly used Python list built-in methods example 8
int_list = [400, 200, 600, 100, 150, 500, -60, 0]
print("\nElement of integer list ->", int_list)
print("\nAny of the elements in integer list is True ? ->", any(int_list))
str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0]
print("\nElement of string list ->", str_list)
print("\nAny of the elements in string list is True ? ->", any(str_list))
mixed_list = [45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0]
print("\nElement of mixed list ->", mixed_list)
print("\nAny of the elements in mixed list is True ? ->", any(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500, -60, 0]
Any of the elements in integer list is True ? -> True
Element of string list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML', 0]
Any of the elements in string list is True ? -> True
Element of mixed list -> [45, -2.6, 'Python', 'Java', 'C++', 20.5, 60, 0]
Any of the elements in mixed list is True ? -> True
any() function will return False only when the list is empty or all the list elements are 0.
# Commonly used Python list built-in methods example 9
int_list = []
print("\nElement of integer list ->", int_list)
print("\nAny of the elements in integer list is True ? ->", any(int_list))
mixed_list = [0, 0, 0, 0]
print("\nElement of mixed list ->", mixed_list)
print("\nAny of the elements in mixed list is True ? ->", any(mixed_list))
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> []
Any of the elements in integer list is True ? -> False
Element of mixed list -> [0, 0, 0, 0]
Any of the elements in mixed list is True ? -> False
list() FUNCTION
list() function is used to create a list with the arguments passed through it. The arguments must be an iterable item. It can be a string or a tuple (Tuples are ordered collection of elements) or any complex data type which can be iterable like list, dictionary, set etc. Iterable means that we can loop through the item. Integer is not iterable, but in python we can loop through a string, as we will see in the below example.
# Commonly used Python list built-in methods example 10
b = 'Python'
print("\nValue of b ->", b)
list_b = list(b)
print("\nElement of list_b ->", list_b)
int_tuple = (400, 200, 600, 100, 150, 500)
int_list = list(int_tuple)
print("\nElement of integer list ->", int_list)
PYTHON LIST BUILT-IN FUNCTIONS : Output
Value of b -> Python
Element of list_b -> ['P', 'y', 't', 'h', 'o', 'n']
Element of integer list -> [400, 200, 600, 100, 150, 500]
# Commonly used Python list built-in methods example 11
int_tuple = (400, 200, 600, 100, 150, 500)
int_list = list(int_tuple)
print("\nElement of integer list ->", int_list)
float_tuple = (4.2, 2.6, 6.5, 1.8, 15.2, 5.3)
float_list = list(float_tuple)
print("\nElement of floating list ->", float_list)
str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')
str_list = list(str_tuple)
print("\nElement of string list ->", str_list)
empty_tuple = ()
empty_list = list(empty_tuple)
print("\nElement of empty list ->", empty_list)
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of integer list -> [400, 200, 600, 100, 150, 500]
Element of floating list -> [4.2, 2.6, 6.5, 1.8, 15.2, 5.3]
Element of string list -> ['Python', 'Java', 'C++', 'C', 'PHP', 'XML']
Element of empty list -> []
If we pass an empty tuple or string as a parameter to list() function then python will create an empty list.
range() FUNCTION
range() function creates a sequence of numbers with in a given range and criteria. It takes three arguments as input.
- Start > It indicates the starting number of the range
- End > It indicates the end number of the range
- Step > It indicates the increment or decrement criteria
Using Positive Step Size
# Commonly used Python list built-in methods example 12
odd_range = range(1, 20, 2)
odd_list = list(odd_range)
print("\nElement of odd integer list ->", odd_list)
even_range = range(2, 20, 2)
even_list = list(even_range)
print("\nElement of even integer list ->", even_list)
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of odd integer list -> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Element of even integer list -> [2, 4, 6, 8, 10, 12, 14, 16, 18]
Using Negative Step Size
# Commonly used Python list built-in methods example 13
odd_range = range(20, 1, -2)
odd_list = list(odd_range)
print("\nElement of odd integer list in descending order -> ", odd_list)
even_range = range(20, 2, -2)
even_list = list(even_range)
print("\nElement of even integer list in descending order -> ", even_list)
PYTHON LIST BUILT-IN FUNCTIONS : Output
Element of odd integer list in descending order -> [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
Element of even integer list in descending order -> [20, 18, 16, 14, 12, 10, 8, 6, 4]
RELATED TOPICS:
- INTRODUCTION TO PYTHON LISTS
- ACCESSING PYTHON LISTS
- ADDING NEW ELEMENTS IN THE LISTS
- UPDATING PYTHON LISTS
- REMOVING ELEMENT IN THE LISTS
- PYTHON LIST SLICING OPERATION
- USING STEP SIZE IN LIST SLICING OPERATION
- LIST SORTING OPERATIONS
- REMOVING DUPLICATES USING PYTHON SET OPERATOR
- PYTHON SHALLOW COPY OPERATIONS
- PYTHON LIST DEEP COPY