Python Forum
Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int (/thread-17457.html)



Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - sr12 - Apr-11-2019

def function_1(x):
    if x < 0:
        return -1000000000
    else:
        return (x ** (1-2)-1)/(1-2)

list = [1, 2, 3, 4,]

[function_1(i) for i in list] 
so I have a function (with a condition as above) -- the intent is to apply that function to each number in a list (and get a new list with the result). Getting stuck here with error: "TypeError: '<' not supported between instances of 'list' and 'int'"

all help appreciated


RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - Yoriz - Apr-11-2019

The code shown doesn't produce the error shown
def function_1(x):
    if x < 0:
        return -1000000000
    else:
        return (x ** (1-2)-1)/(1-2)
 
my_list = [1, 2, 3, 4,]
 
print([function_1(i) for i in my_list])
Output:
[-0.0, 0.5, 0.6666666666666667, 0.75]
Note: don't over write the built in list and please post the full error trace back when you have an error.


RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - ichabod801 - Apr-11-2019

I get no error running your code.

Also, you shouldn't use list as a variable name. That name is a built-in, and by using it as a variable name you lose access to the built-in.


RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - sr12 - Apr-11-2019

sorry was trying to recreate in a generic way when I'm getting error in a larger file. added a few steps and am now getting error again:

def function_1(x):
    if x < 0:
        return -1000000000
    else:
        return (x ** (1-2)-1)/(1-2)
  
my_list = [1.064, .4546, .998, .777,]
my_list2 = [2, 3, -5]
my_list3 = []

for x in my_list2:
    my_list3.append([x * i for i in my_list])

print([function_1(i) for i in my_list3])

  
Output:
runfile('C:/Users/xxxxx/Documents/untitled1.py', wdir='C:/Users/xxxxx/Documents') Traceback (most recent call last): File "<ipython-input-120-55d0d62f9a9e>", line 1, in <module> runfile('C:/Users/ssxxxx/Documents/untitled1.py', wdir='C:/Users/xxxxx/Documents') File "C:\Users\xxxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace) File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/xxxx/Documents/untitled1.py", line 22, in <module> print([function_1(i) for i in my_list3]) File "C:/Users/xxxx/Documents/untitled1.py", line 22, in <listcomp> print([function_1(i) for i in my_list3]) File "C:/Users/xxxx/Documents/untitled1.py", line 10, in function_1 if x < 0: TypeError: '<' not supported between instances of 'list' and 'int'



RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - buran - Apr-11-2019

my_list3 is list of lists, so on line 14 when you iterate over my_list3 i is a list, i.e. you pass a list as argument to function_1


RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - Yoriz - Apr-11-2019

def function_1(x):
    if x < 0:
        return -1000000000
    else:
        return (x ** (1-2)-1)/(1-2)
   
my_list = [1.064, .4546, .998, .777,]
my_list2 = [2, 3, -5]
my_list3 = []
 
for x in my_list2:
    my_list3.append([x * i for i in my_list])
    
print(my_list3)
 
# print([function_1(i) for i in my_list3])
Output:
[[2.128, 0.9092, 1.996, 1.554], [3.192, 1.3638, 2.9939999999999998, 2.331], [-5.32, -2.273, -4.99, -3.8850000000000002]]
my_list3 has lists nested inside the list, so the x passed to def function_1(x) is a list


RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - sr12 - Apr-11-2019

OK yes, agree there. So how can I apply function_1 to each item in the list of lists? (keeping it in list of lists format)


RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - Yoriz - Apr-11-2019

def function_1(x):
    if x < 0:
        return -1000000000
    else:
        return (x ** (1 - 2) - 1) / (1 - 2)

   
my_list = [1.064, .4546, .998, .777, ]
my_list2 = [2, 3, -5]
my_list3 = []
 
for x in my_list2:
    my_list3.append([x * i for i in my_list])

print([[function_1(i) for i in inner_list] for inner_list in my_list3])
Output:
[[0.5300751879699248, -0.09986801583809934, 0.498997995991984, 0.3564993564993565], [0.68671679197995, 0.2667546561079337, 0.665998663994656, 0.570999570999571], [-1000000000, -1000000000, -1000000000, -1000000000]]



RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - sr12 - Apr-11-2019

Thank you!