In your original code, you are iterating over the elements of the sublist and you are using them as indexes, since the element of the sublist is a float, when you use them as an index i.e. you do tumor_size[2.6], hence you get the error TypeError: list indices must be integers or slices, not float

Also it should be and instead of & to chain conditionals together

To fix this, you would need to iterate over the sublists of each list, and calculate the max given that the list is non-empty

tumor_size = [[2.6,3.65],[],[2.0,2.9,1.7,2.5,1.3]]

#Find max of each sublist given sublist is non-empty
res = [max(li) for li in tumor_size if li]

print(res)

The output will be

[3.65, 2.9]

Or if we go with your original approach, you can get rid of the inner loop, check for maxlen only if the sublist is non-empty, and do your comparison

tumor_size = [[2.6,3.65],[],[2.0,2.9,1.7,2.5,1.3]]


T_stage_list=[]

for i in tumor_size:

    #Default value is T0
    T_stage = "T0"
    #If sublist is non-empty
    if i:
        #Get max and perform comparison
        maxlen = max(i)
        if (maxlen>2.0 and maxlen<3.0):
            T_stage = "T2a"
        else:
            T_stage = "T2b"
    #Add T_stage
    T_stage_list.append(T_stage)

print(T_stage_list)

The output will be

['T2b', 'T0', 'T2a']
Answer from Devesh Kumar Singh on Stack Overflow
๐ŸŒ
Pythontic
pythontic.com โ€บ containers โ€บ list โ€บ max
max() function of list in Python | Pythontic.com
The max() function returns the maximum of the elements present in a Python list.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to get the max value if that is a float?
r/learnpython on Reddit: how to get the max value if that is a float?
December 6, 2022 -

Hi, I have calculated some values and now need the program to print which is the largest. The problem is that these numbers are floats and I get an error: TypeError: 'float' object is not iterable when using the max function. And I can't change it to int for example, because I need to include numbers after the comma. I would be really grateful if someone could help me.

Discussions

Finding max value in a series of floats
I am trying to write code that takes a row of floats from a csv file, representing the average temperature in a given month over the course of 70 years, and returns the highest float value of them all. When I print the variable holding the float values, in this case val, I get something similar ... More on discuss.python.org
๐ŸŒ discuss.python.org
11
0
April 15, 2024
python - Get the max value of float numbers on a list on tuples? - Stack Overflow
First the array is sorted based ... X.Z with common X. In each of these sequences - groups the program finds the one with maximum decimal part treated as a stand-along number. The whole number is then appended to the list with max values as a float.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 31, 2017
python - How do I find the largest float in a list array? - Stack Overflow
For this, I need the population ... the largest a. I've created row[3] in my list to store a values but I'm unable to have python identify the largest one. I've tried to sort row[3] or simply find the max value, but am told 'float' object is not iterable.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Max value in list with decimals. - Stack Overflow
So, I'm having a lot of trouble finding the largest decimal integer in a massive list of strings (1500ish). Here's what I have within a function (to find the max value): all_data_lines = data.spli... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Finding max value in a series of floats - Python Help - Discussions on Python.org
April 15, 2024 - I am trying to write code that takes a row of floats from a csv file, representing the average temperature in a given month over the course of 70 years, and returns the highest float value of them all. When I print the variable holding the float values, in this case val, I get something similar ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Maximum and Minimum float Values in Python | note.nkmk.me
August 11, 2023 - print(sys.float_info.max) # 1.7976931348623157e+308 ... See the official documentation for a list of items and their explanations.
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ print lowest & highest float in list in python (3 examples)
Print Lowest & Highest Float in Python List | Smallest / Largest Item
May 4, 2023 - This filters out any non-float elements in the original list. Finally, the lowest and highest values are found by the min() and the max() functions, respectively, and printed into the console. Easy! This method shows how to use a for loop to print the lowest and highest float values in a list.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ integer and float maximums in pythonโ€™s max() function
Integer and Float Maximums in Python's Max() Function - Be on the Right Side of Change
September 15, 2019 - print(max([5, 10, 10.0])) # 10 print(max([5, 10.0, 10])) # 10.0 ยท It simply returns the highest value in an iterable. Note that many advanced coders worry that we cannot compare an int with a float because of limited floating point precision because they know the following example: print(0.3+0.3+0.3 == 0.9) # False print(0.3+0.3+0.3) # 0.8999999999999999 ยท This is not an issue when comparing integers with โ€œinteger-likeโ€ floats. Python floats can represent integers perfectly fine.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-min-max-value-in-float-string-list
Python | Min/Max value in float string list - GeeksforGeeks
April 8, 2023 - Compare the float value to the current maximum value. If the value is greater than the current maximum value or if the maximum value is None, update the maximum value. After iterating through the entire list, the minimum and maximum values will be stored in their respective variables. Print the minimum and maximum values. Below is the implementation of the above approach: ... # Python3 code to demonstrate working of # Min / Max value in float string list # using for loop and float() # initialize list test_list = ['4.5', '7.8', '9.8', '10.3'] # printing original list print("The original list is
Find elsewhere
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python-max-function
Linux Hint โ€“ Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Bomberbot
bomberbot.com โ€บ python โ€บ mastering-min-and-max-in-python-a-deep-dive-into-floats-strings-and-lists
Mastering Min and Max in Python: A Deep Dive into Floats, Strings, and Lists - Bomberbot
This example finds the minimum and maximum values based on their absolute values, demonstrating how flexible Python can be in handling complex comparison scenarios. The custom_compare function defines the comparison logic, which is then used by cmp_to_key to create a key function for min() and max(). Mastering the techniques for finding minimum and maximum values in lists of float strings is a valuable skill in Python programming.
๐ŸŒ
DEV Community
dev.to โ€บ frankkk โ€บ python-tips-floatinf-269h
Python Tips- Maximum Value float('inf') - DEV Community
February 3, 2024 - For the float, we max value is sys.float_info.max. for the integer, we can use the value sys.maxsize. But Integer (int) has no max limit in Python 3, from the source code in C language, the int object is defined variable size, that is why it ...
๐ŸŒ
Boot.dev
boot.dev โ€บ lessons โ€บ 9a942c38-7ad9-40b7-8f4b-96b3f71d5de2
Learn Python for Beginners: Find Max | Boot.dev
negative_infinity = float("-inf") positive_infinity = float("inf") Our players want a way to see their strongest attack from their last combat. Some enemies may be buffed to absorb either magic or melee attacks as health, which result in negative damage. Let's add another function to analyze data from our combat log. Complete the find_max function. It takes as input a list of integers, nums, and returns a number.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ find maximum float value in python
Find Maximum Float Value in Python - Spark By {Examples}
May 31, 2024 - How to find the maximum float value in Python? You can find the maximum value of the float data type using the sys.float_info module or the finfo()
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ What-is-the-maximum-value-of-float-in-Python
What is the maximum value of float in Python?
The sys.float_info object provides details about the float implementation on the current platform. The attribute float_info.max gives the largest positive floating-point number representation.
๐ŸŒ
Vegibit
vegibit.com โ€บ python-max-function
Python max() Function
You can use grid-column and grid-row to explicitly place your items. If you want an item to start on column line 1 and end on column line 3, that's grid-column: 1 / 3. Or maybe you want an item to span 2 columns wherever it would naturally land: grid-column: span 2.
๐ŸŒ
Finxter
blog.finxter.com โ€บ setting-a-python-float-to-its-maximum-possible-value
Setting a Python Float to Its Maximum Possible Value โ€“ Be on the Right Side of Change
February 25, 2024 - It is a concrete value close to the upper boundary of a floatโ€™s range in Python. ... This code uses the max attribute of sys.float_info to set max_float to the highest possible finite floating-point value supported by the Python interpreter.