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]) 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])
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])
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])
RE: Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int - sr12 - Apr-11-2019 Thank you! |