Python Forum
Example of list comprehensions doesn't work
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example of list comprehensions doesn't work
#4
(May-10-2018, 11:19 PM)Truman Wrote:
x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if (((i+j) !=n)))
The goal is to print all possible coordinates.
I receive a generator object message. Don't understand why.

Add another set of angular brackets in a print statement - within and next to outermost brackets.

And
(((i+j) !=n))
is extremely ugly and un-Pythonic.

i + j != n
is the proper Pythonic form. I sentence you to read PEP-8 Idea
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
RE: Example of list comprehensions doesn't work - by volcano63 - May-11-2018, 07:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 364 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Why doesn't list require global keyword? johnywhy 9 1,219 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,092 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,578 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 973 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,087 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  How to work with list kafka_trial 8 2,213 Jan-24-2023, 01:30 PM
Last Post: jefsummers
  color code doesn't work harryvl 1 1,035 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,705 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,870 Jun-16-2022, 07:59 AM
Last Post: gerald

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020