+ 3
How to get negative values from A = [1,3, -2, 5, -7, -8, 3, 12, "Addy"] using python?
20 odpowiedzi
+ 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.
+ 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*.
+ 3
Use list comprehension
x = [i for i in A if i<0]
+ 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..
+ 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
+ 2
Thank you so much bro Kishor Ramanan . Take ❤️
+ 2
Thank you so much Kendra ❤️
+ 1
Bro, I tried many ways to get negative value from it, but I failed. Please help me if you can.
+ 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=' ')
+ 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
+ 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.
+ 1
Lothar I'm on that path, I need your help if I get stuck somewhere.
+ 1
list(filter(lambda x: isinstance(x, (int,float)) and x<0, A))
+ 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
0
A=[1,3,-2]
index=0
while True:
b=(A[index])
if b<0:
print(b)
index+=1
0
Easy way:
for i in A:
if i<0:
print(i)
0
Hii
0
Dual filters ☺️
for i in A:
if type(i)==int:
if i<0:
print(i)
else:
continue