Python Forum

Full Version: if and for statements on the same line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello friends,
I have a very easy question but since I am a beginner I could not solve it.
I have a list like
a=[1 2 3 -4]
what I wanted to do
if (condition x>condition y and all the elements of list a is greater than 0)
do this
else:
do that
how can I check the each element of list a if they are greater than 0 on the "if" line?
Thank you so much
I tried to to do something like if (condition x>condition y and [n for n in a >0]:
but it did not work
I will be very happy, if you can help me
if conditionx > conditiony and all(n > 0 for n in a):

or

if conditionx > conditiony and all(map(lambda x: x >0, a)):

the first one is better
Thank you so much buran , it worked :)
Hello Buran ,
I have another similar question . This time I have an matrix like
C=[[1 2 3]
[4 5 6]
[7 8 9]
I need to choose second line [4 5 6] and check if each element is greater than 10. (check 4>10 ,5>10,6>10) and I have to write it in rhe same same with the previous idea . Let's say
if a>b and first element of the second line is greater than 10. (until third element of second line )
How can I write it ?
Thank you so much
(Sep-17-2018, 08:17 AM)juniorcoder Wrote: [ -> ]Hello Buran ,
I have another similar question . This time I have an matrix like
C=[[1 2 3]
[4 5 6]
[7 8 9]
I need to choose second line [4 5 6] and check if each element is greater than 10. (check 4>10 ,5>10,6>10) and I have to write it in rhe same same with the previous idea . Let's say
if a>b and first element of the second line is greater than 10. (until third element of second line )
How can I write it ?
Thank you so much
Asked in three places!