SyntaxError invalid syntax in inline function

0 votes

Hi Guys,

I am trying to perform one task using the list comprehension concept. But it is showing the syntax error.

  File "<ipython-input-9-f0c31b619ddf>", line 1
    m = [i*i for i in x if i>0 else 0]
                                 ^
SyntaxError: invalid syntax
Oct 5, 2020 in Python by akhtar
• 38,210 points
342 views

1 answer to this question.

0 votes

Hi@akhtar,

You need to write your if-else code before the for loop. But in your case, you have written after for loop. So you need to modify your code as shown below.

m = [i*i  if i>0 else 0 for i in x]
answered Oct 5, 2020 by MD
• 95,320 points

Related Questions In Python

0 votes
2 answers

%matplotlib inline SyntaxError: invalid syntax

I found a good solution to solve ...READ MORE

answered May 2, 2020 in Python by Edu
15,023 views
0 votes
1 answer

Python error "SyntaxError: invalid syntax"

but i m getting this ouput: Enter the ...READ MORE

answered Mar 20, 2020 in Python by anonymous
6,941 views
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
405 views
0 votes
1 answer

What does ' -> ' mean in Python function definitions?

It's a function annotation. In more detail, Python 2.x ...READ MORE

answered May 23, 2018 in Python by charlie_brown
• 7,780 points
286 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,472 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,854 views
0 votes
1 answer

SyntaxError: invalid syntax print worldlists[len(words)]

Hi@akhtar, This syntax will not allow in all ...READ MORE

answered Jun 30, 2020 in Python by MD
• 95,320 points
460 views
0 votes
2 answers