0

For some reason i can not understand,a TypeError occures when using min and max functions.. Here is the code: import random from typing import List, Any, Union

list1 = []

for i in range(0,200):
    x = random.randint(1,50)
    list1.append(x)

list2 = [ j for j in range(0,51)]

list1.append(list2)
print(list1)

del list1[99:130]

print(len(list1))

print(min(list1))

And this is the error:TypeError: '<' not supported between instances of 'list' and 'int'

Thank you :)

Pluto
  • 853
  • 1
  • 8
  • 28
Play
  • 5
  • 5

2 Answers2

0

it's because you have a nested list (list1) see on the example

f = [12,3,4,5,[1,2,3]]
In [25]: min(f)                                                                                                                                                                                             
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-d9448f4534d8> in <module>
----> 1 min(f)

TypeError: '<' not supported between instances of 'list' and 'int'

Beny Gj
  • 607
  • 4
  • 16
0

list1.append(list2) adds list2 (which is indeed a list) as an additional element to the end of list1. Your error arises because Python can't find the minimum of a list which consists of several numbers and one list.

I think you wanted to add all the individual numbers in list2 to list1, to end with a list of just numbers. This can be done using extend, rather than append.

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34