List Comprehension Help

This is the place for queries that don't fit in any of the other categories.

List Comprehension Help

Postby Indiiyana » Wed Jan 15, 2014 8:42 am

Hi,

I was reading a tutorial and it had the following code:
Code: Select all
# gives [(0, 0), (0, 1), (1, 0), (1, 1)]
[(x, y) for x in range(2) for y in range (2)]


Also, how can I print this list out onto the screen?

How does the counting work in this case? That's the bit I'm confused about. If somebody could walk through it step-by-step that would be great, thanks.
Indiiyana
 
Posts: 10
Joined: Tue Nov 26, 2013 1:55 am

Re: List Comprehension Help

Postby stranac » Wed Jan 15, 2014 9:49 am

These two pieces of code result in the same list:
Code: Select all
some_list = [(x, y) for x in range(2) for y in range(2)]

Code: Select all
some_list = []
for x in range(2):
    for y in range(2):
        some_list.append((x, y))

You print it using print, just like you would anything else...

More information in the docs.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: List Comprehension Help

Postby XndrK » Sat Jan 18, 2014 7:28 pm

stranac wrote:These two pieces of code result in the same list:
Code: Select all
some_list = [(x, y) for x in range(2) for y in range(2)]

Code: Select all
some_list = []
for x in range(2):
    for y in range(2):
        some_list.append((x, y))

You print it using print, just like you would anything else...

More information in the docs.


You mean this, right?

Code: Select all
some_list.append((x, y))
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm

Re: List Comprehension Help

Postby stranac » Sat Jan 18, 2014 7:38 pm

I have no idea what you're talking about...
You must be confused. I never make mistakes, I am an admin. :twisted:

But yes, that is what I meant. That's why I have written it, and it's what you quoted.
Thanks anyway. :twisted:
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: List Comprehension Help

Postby XndrK » Sat Jan 18, 2014 8:17 pm

I could have sworn you put in some_list.append(2)!
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm

Re: List Comprehension Help

Postby metulburr » Sat Jan 18, 2014 9:11 pm

I could have sworn you put in some_list.append(2)!

haha

admins and moderators can change posts without it displaying that they changed the post. If stranac did typo it, then im sure he changed it to make it less confusing for the OP.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1295
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: List Comprehension Help

Postby XndrK » Mon Jan 20, 2014 5:41 pm

Oh.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest