. Advertisement .
..3..
. Advertisement .
..4..
In Python, the highest-valued member in a list is the max value many are having trouble looking for.
The question here is: How can we acquire a Python max list without paying too much effort tracking down each element?
Scroll down to find out your desirable quick fix!
How To Acquire A Python Max Of List?
There are three approaches in total that you can utilize to get the task of figuring out a Python max of list done successfully. Please take a look at the details below to see what they are!
Use Max() And List.Index() To Determine The Max Value In A List
The first method to come is to employ max() and list.index() to determine the max value in a list.
As such, with a list as iterable, call max(iterable) to get the list’s maximum value. In terms of discovering the index of its first occurrence, we may use list.index(element) with the max value as the element.
Running the code:
example_listNum = [25, 22, 28, 19, 65, 72]
max_number = max(example_listNum)
max_of_list = example_listNum.index(max_number)
print(max_number)
print(max_of_list)
Output:
72
5
Use The for Loop To Determine The Max Value In A List
Another approach not yet mentioned is the for Loop. Following the instruction below to get your asked value:
- Step 1: Make the first element the contender with the highest number.
- Step 2: Iterate over the list of numbers.
- Step 3: If there are any numbers greater than the former, continue to update the element with the highest number.
Running the code:
list_number = [1878, 1902, 171, 19, 6817, 1882, 7, 23]
max_value = list_number[0]
for i in list_number:
if i > max_value:
max_value = i
print('The max value In a list is:',max_value)
Output:
The max value In a list is: 6817
Determine Max Value in a Nested List in Python
Determining the maximum value of a nested list in Python is a more sophisticated example. Let’s start with a hierarchical list of random values and an in-depth guide to complete the task as follow:
arr_num = [[1878, 0.30916], [1890, 0.30967], [1902, 0.30122]]
- Step 1: So, what do we actually want now? Extracting the maximum value of the second member from this nested list is the first step, indeed!
- Step 2: Then, you may wish to publish the first element and use it as an index.
- Step 3: Within a list, the nested list is handled more like a key-value pair.
- Step 4: We may use the method max() with its optional argument key to get the maximum value of this complicated structure, which takes note of the element inside the nested list to compare. That way, the key parameter takes a lambda function as an input, allowing max() to return a key-value pair.
Running the code:
arr_num = [[1878, 0.30916], [1890, 0.30967], [1902, 0.30122]]
sec, maxValueOfList= max(arr_num, key=lambda item: item[1])
print('The max value:', maxValueOfList, "At section:",sec)
Output:
The max value: 0.30967 At section: 1890
Conclusion
Above is all that you need to grasp regarding how to get a Python Max of list. Now, it’s your moment to go on the road and give it a try yourself. Don’t forget to leave a comment if there’s anything yet to confuse you. See then!
Leave a comment