for loop in one line in Python

0 votes

When I tried to use a for statement in one line like below,

def get_cubes(x):
ls=[]
ls.append(pow(item*3, 3)) for item in range(int((x-x%3)/3)+1)
return ls

there was always an error:

File "<ipython-input-47-8c391c3d568a>", line 3
ls.append(pow(item*3, 3))  for item in range(int((x-x%3)/3)+1)
                             ^
SyntaxError: invalid syntax

However, it works well when I write it using a standard for loop method. Why?

Nov 24, 2022 in Python by sarit
• 1,830 points
364 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

How to use for loop in Python?

There are multiple ways of using for ...READ MORE

answered Mar 4, 2019 in Python by Priyaj
• 58,090 points
470 views
0 votes
1 answer

How can I deal with python eggs for multiple platforms in one location?

Try virtualenv : http://pypi.python.org/pypi/virtualenv This helps you create isolated ...READ MORE

answered May 28, 2019 in Python by SDeb
• 13,300 points
686 views
0 votes
1 answer

How can I change the iteration variable inside a for loop in python?

Assume you have a list of lists: my_list ...READ MORE

answered Jun 10, 2019 in Python by Shubham Bansal
5,253 views
+1 vote
1 answer

Why does print() run only after executing for loop in Python?

Your stdout is line-buffered; this means that it won't ...READ MORE

answered Aug 1, 2019 in Python by Neel
• 3,020 points
1,347 views
0 votes
0 answers

What is the syntax for an empty while loop in python?

What kind of ambiguities are expected for ...READ MORE

Aug 5, 2019 in Python by Waseem
• 4,540 points
584 views
0 votes
2 answers

How to use in python for loop not equal marks? example: a!=0

Hello @Azizjon, You can go through the example ...READ MORE

answered Oct 12, 2020 in Python by Gitika
• 65,910 points
1,896 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 4,007 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP