Python Forum
Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int
#1
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
Reply
#2
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.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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'
Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
Reply
#7
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)
Reply
#8
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]]
Reply
#9
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 690 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 674 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 592 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 960 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,172 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,846 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,139 May-07-2022, 08:40 AM
Last Post: ibreeden
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,566 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Noobie Python TypeError question zoid 4 7,819 Sep-01-2021, 02:28 PM
Last Post: zoid
  Question from complete python's newbie Davicom 3 2,357 Jun-09-2021, 06:09 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020