Updated March 30, 2023
Definition of Python 3 List Function
Python 3 list functions is a fundamental concept used in python. In Python, the sequence is the most fundamental data type. A number is allocated to the sequence element its index or location. The initial index is zero, followed by one, two, and so on. The most popular sequences in Python are lists and tuples. Python also includes functions for determining a sequence of length as well as the largest and smallest items.
Python 3 List Functions
- With all sequence kinds, there are several things we can do. Indexing determining membership are just a few of the operations available.
- The list function in Python is the most versatile, and it can be expressed list can be a comma-separated value enclosed by square brackets. A list’s most important feature is that its items do not have to be of the same type.
- Putting various comma-separated items between square brackets is all it takes to make a list.
Below is the list functions available in python.
1. Len
- By invoking the list object’s own length method. It accepts a list object as input and has no effect on the list.
- The len method can be applied to any sequence or collection (such as a text, bytes, tuple, list or range).
- The below example shows the len function in python 3.
Code:
stud_name = ["ABC", "PQR", "XYZ"]
print(len(stud_name))
Output:
2. List
- Below example shows python 3 list. In below example we have printed 3 list, first contains the string values, the second contains the integer values and the third also contains the string value.
Code:
py_list1 = ['ABC', 'PQR', 'XYZ'];
py_list2 = [11, 13, 15];
py_list3 = ["p", "q", "r"];
print (py_list1)
print (py_list2)
print (py_list3)
Output:
- We can access the specified values from list. To get values from lists, use square brackets and the index or indices to get the value at that index.
- Below example shows how to access the values from list. We are creating two list, in first list we are accessing only a single element from list. But in second list we are accessing elements from list in sequence.
Code:
py_list1 = ['ABC', 'PQR', 'XYZ'];
py_list2 = [11, 13, 15, 17, 19, 21, 23, 25];
print ("py_list1[0]: ", py_list1[0])
print ("py_list2[1:5]: ", py_list2[1:5])
Output:
3. Max
- The largest item in an iterable is returned. Argument of key will specify a sort-like one-argument ordering algorithm.
- If the supplied iterable is empty, the default argument defines an object to return. A Value Error is thrown if iterable contains the empty value and no default is specified. Only the first item found is returned item has the same maximum value.
- The below example shows max function in the python 3 list are as follows.
Code:
py_max = ["ABC", "PQR", "XYZ"]
print (max(py_max))
py_max = ["ABC", "PQR", "XYZ"]
print (max(py_max))
py_max = [11, 13, 15, 17, 19, 21]
py_max1 = [11, 13, 15, 17]
print (max(py_max, py_max1))
Output:
4. Min
- The smallest item in an iterable is returned. Argument of key will specifies a sort-like one-argument ordering algorithm.
- If the supplied iterable is empty, the default argument defines an object to return. A Value Error is thrown if iterable contains the empty value and no default is specified. Only the first item found is returned item has the same minimum value.
- The below example shows the min function in python 3 list are as follows.
Code:
py_min = ["ABC", "PQR", "XYZ"]
print (min(py_min))
py_min = ["ABC", "PQR", "XYZ"]
print (min(py_min))
py_min = [11, 13, 15, 17, 19, 21]
py_min1 = [11, 13, 15, 17]
print (min(py_min, py_min1))
Output:
5. Range
- Represents an immutable series of numbers, which is typically used in for loops to loop a certain amount of times.
- It can be used in conjunction with list to generate a list of items that fall within a specified range. Range is a sequence type that can be changed.
- Below is the example of range function in python is as follows.
Code:
print (list (range(11)))
print (list (range(11, 13)))
print (list (range(61, 63)))
print (list (range(71, 73, 75)))
Output:
6. Sum
- The sum function in Python adds all of the items and it will returning the total. From left to right, the iterable includes objects to be added. Start is a positive value returning.
- The items and start of the iterable should both be integers. If no value is specified for start. Below is the syntax of sum function in python is as follows.
Sum (iterable[,start])
- The below example shows the sum function in python is as follows. In the below example, we have used sum function to return the total of all values.
Code:
def py_loop (py_items, py_start):
py_total = py_start
for item in py_items:
py_total += item
return py_total
if __name__ == '__main__':
py_items = [11, 13, 15, 19, 17, -11]
py_start = 11
print ("Sum is:", py_loop (py_items, 11))
Output:
7. Sorted
- The Python sorted function takes an iterable and returns an object. A list of objects will be iterable here.
- The function key specifies a one-argument function that extracts a comparison element. Reverse is a bool that defines ascending or descending order.
- Below is the example of sorted function in python are as follows.
Code:
py_num = [14, 13, 10, 16, 23, 19, 10]
print(sorted(py_num))
Output:
8. Reverse
- The reversed function in Python returns a reverse iterator, from which we have requested.
- Below example shows the reverse function in python are as follows.
Code:
py_list = [11, 12, 14, 13, 15]
print (list (reversed (py_list)))
Output:
9. Map
- The map method in Python returns an iterator that which was mapped function of each iterable items.
- When we don’t want to utilise the usual for loop, yet to each item in iterables, we use this function.
- Below is the example of map function in python are as follows.
Code:
def addition (py):
return py + py
py_map = [16, 14, 18, 19, 12, 13, 19]
py_res = map (addition, py_map)
print (list(py_res))
Output:
Conclusion
The list function in Python is the most versatile, and it can be expressed list can be a comma-separated values enclosed by square brackets. Python 3 list functions is a fundamental concept used in python. In Python, the sequence is most fundamental data type.
Recommended Articles
This is a guide to Python 3 List Function. Here we discuss the definition, Python 3 List Functions, examples with code implementation. You may also look at the following articles to learn more-