+ 3

How to get negative values from A = [1,3, -2, 5, -7, -8, 3, 12, "Addy"] using python?

18th Dec 2022, 1:05 PM
Pronoy Roy
Pronoy Roy - avatar
20 Réponses
+ 10
Kishor Ramanan , Kendra , it is not seen as a helpful behavior when we are going to post a code, as long as the op has not shown his attempt here. it is helpful to give hints and tips, so that the op has a chance to find a solution by himself.
18th Dec 2022, 7:22 PM
Lothar
Lothar - avatar
+ 5
Pronoy Roy , from my point of view you may miss some basics of python. since the task you have not finished is basic stuff, i would recommend to learn from the tutorial *python for beginners*.
18th Dec 2022, 8:45 PM
Lothar
Lothar - avatar
+ 3
Use list comprehension x = [i for i in A if i<0]
18th Dec 2022, 2:44 PM
Kendra
Kendra - avatar
+ 3
bro yasas chamara, Rick Zalman there is also a string in the list, if you do it like this, you will get an error..
20th Dec 2022, 11:55 AM
Pronoy Roy
Pronoy Roy - avatar
+ 2
you can iterate through the list and check each element if it is a number and that number is < 0 then that's negative value you can store it or print it
18th Dec 2022, 1:10 PM
Kishor Ramanan
Kishor Ramanan - avatar
+ 2
Thank you so much bro Kishor Ramanan . Take ❤️
18th Dec 2022, 1:39 PM
Pronoy Roy
Pronoy Roy - avatar
+ 2
Thank you so much Kendra ❤️
19th Dec 2022, 5:16 AM
Pronoy Roy
Pronoy Roy - avatar
+ 1
Bro, I tried many ways to get negative value from it, but I failed. Please help me if you can.
18th Dec 2022, 1:25 PM
Pronoy Roy
Pronoy Roy - avatar
+ 1
a = [1,3, -2, 5, -7, -8, 3, 12, "Addy"] # iterate through the list for x in a: # checks if x datatype is int and x < 0 if isinstance(x , int) and x < 0: print(x, end=' ')
18th Dec 2022, 1:30 PM
Kishor Ramanan
Kishor Ramanan - avatar
+ 1
Lothar I have mentioned how to do it by hints on the 1st answer but that code is self-explained and I used comments so I think it's helpful to him
18th Dec 2022, 7:29 PM
Kishor Ramanan
Kishor Ramanan - avatar
+ 1
Lothar I said earlier that I tried a lot to solve it but couldn't. I posted because I couldn't find any other way. I tried my best.
18th Dec 2022, 7:42 PM
Pronoy Roy
Pronoy Roy - avatar
+ 1
Lothar I'm on that path, I need your help if I get stuck somewhere.
19th Dec 2022, 5:05 AM
Pronoy Roy
Pronoy Roy - avatar
+ 1
list(filter(lambda x: isinstance(x, (int,float)) and x<0, A))
19th Dec 2022, 11:18 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
You should try to loop through that array and than for each iteration compare it to 0. Hope it helps :) noticed there is a string on the list you should also first loop and remove anything that is not a int
20th Dec 2022, 12:29 PM
José Nogueira
José Nogueira - avatar
0
A=[1,3,-2] index=0 while True: b=(A[index]) if b<0: print(b) index+=1
20th Dec 2022, 10:07 AM
yasas chamara
yasas chamara - avatar
0
Easy way: for i in A: if i<0: print(i)
20th Dec 2022, 10:28 AM
yasas chamara
yasas chamara - avatar
0
Hii
20th Dec 2022, 12:49 PM
Guru
Guru - avatar
0
Dual filters ☺️ for i in A: if type(i)==int: if i<0: print(i) else: continue
20th Dec 2022, 12:51 PM
yasas chamara
yasas chamara - avatar