What is a list compression in Python?

K

Kit

Hello Everyone, I am not sure if I have posted this question in a
correct board. Can anyone please teach me:

What is a list compression in Python?

Would you mind give me some list compression examples?


Thanks & really appreciate that.
Kit
 
S

Steven D'Aprano

Hello Everyone, I am not sure if I have posted this question in a
correct board. Can anyone please teach me:

What is a list compression in Python?

Google "python list comprehension".

If Google is broken for you, try Yahoo, or any other search engine.

Would you mind give me some list compression examples?

Instead of this:

L = []
for x in range(10):
L.append(x**2)


you can write:

L = [x**2 for x in range(10)]


Instead of this example:


L = []
for x in range(10):
if x % 2 == 0:
L.append(x**2)



you can write:

L = [x**2 for x in range(10) if x % 2 == 0]
 
K

Kit

Thank you so much guys.

Just out of curiosity: can I do something like this to "square all
even numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]

Or is there a better way of doing it? Thanks for the help, and I am
really appreciate your help.

Kit.

Hello Everyone, I am not sure if I have posted this question in a
correct board. Can anyone please teach me:
What is a list compression in Python?

Google "python list comprehension".

If Google is broken for you, try Yahoo, or any other search engine.
Would you mind give me some list compression examples?

Instead of this:

L = []
for x in range(10):
    L.append(x**2)

you can write:

L = [x**2 for x in range(10)]

Instead of this example:

L = []
for x in range(10):
    if x % 2 == 0:
        L.append(x**2)

you can write:

L = [x**2 for x in range(10) if x % 2 == 0]
 
K

Kit

Oops...
print [x^2 for x in range (1,11) if x % 2 == 0]
print [x^2 for x in range (1,10) if x % 2 == 0]


Thank you so much guys.

Just out of curiosity: can I do something like this to "square all
even numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]

Or is there a better way of doing it? Thanks for the help, and I am
really appreciate your help.

Kit.

Google "python list comprehension".
If Google is broken for you, try Yahoo, or any other search engine.
Instead of this:
L = []
for x in range(10):
    L.append(x**2)
you can write:
L = [x**2 for x in range(10)]
Instead of this example:
L = []
for x in range(10):
    if x % 2 == 0:
        L.append(x**2)
you can write:
L = [x**2 for x in range(10) if x % 2 == 0]
 
M

MRAB

Kit said:
Thank you so much guys.

Just out of curiosity: can I do something like this to "square all
even numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]

Or is there a better way of doing it? Thanks for the help, and I am
really appreciate your help.
That would be:

print [x ** 2 for x in range(1, 11) if x % 2 == 0]

Note that Python uses "**" for raising to a power and "^" (like in C)
for bitwise exclusive-or.
 
G

Gary Herron

Kit said:
Thank you so much guys.

Just out of curiosity: can I do something like this to "square all
even numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]

Or is there a better way of doing it? Thanks for the help, and I am
really appreciate your help.

That's a fine way to do it, although:

xrange is slightly more efficient than range for large
sets of values. (This isn't such a large set for it to matter.)

You can get range and xrange to count by two's, eliminating
the need for the "if" portion in this case.

The exponent operator is ** not ^, and x*x is more efficient than x**2.

So.

print [x**2 for x in xrange(1,11,2)]

Gary Herron


Kit.

Hello Everyone, I am not sure if I have posted this question in a
correct board. Can anyone please teach me:

What is a list compression in Python?
Google "python list comprehension".

If Google is broken for you, try Yahoo, or any other search engine.

Would you mind give me some list compression examples?
Instead of this:

L = []
for x in range(10):
L.append(x**2)

you can write:

L = [x**2 for x in range(10)]

Instead of this example:

L = []
for x in range(10):
if x % 2 == 0:
L.append(x**2)

you can write:

L = [x**2 for x in range(10) if x % 2 == 0]
 
K

Kit

Cool! Thank you very much. You mean this instead right?

print [x*x for x in xrange(1,11,2)]

Kit


Kit said:
Thank you so much guys.
Just out of curiosity: can I do something like this to "square all
even numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]
Or is there a better way of doing it? Thanks for the help, and I am
really appreciate your help.

That's a fine way to do it, although:

  xrange is slightly more efficient than range for large
  sets of values.  (This isn't such a large set for it to matter.)

  You can get range and xrange to count by two's, eliminating
  the need for the "if" portion in this case.

  The exponent operator is ** not ^, and  x*x is more efficient than x**2.

So.

print [x**2 for x in xrange(1,11,2)]

Gary Herron


On 1月19æ—¥, 上åˆ12時30分, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
On Mon, 18 Jan 2010 08:07:41 -0800, Kit wrote:
Hello Everyone, I am not sure if I have posted this question in a
correct board. Can anyone please teach me:
What is a list compression in Python?
Google "python list comprehension".
If Google is broken for you, try Yahoo, or any other search engine.
Would you mind give me some list compression examples?
Instead of this:
L = []
for x in range(10):
    L.append(x**2)
you can write:
L = [x**2 for x in range(10)]
Instead of this example:
L = []
for x in range(10):
    if x % 2 == 0:
        L.append(x**2)
you can write:
L = [x**2 for x in range(10) if x % 2 == 0]
 
M

MRAB

Kit said:
Oops...
print [x^2 for x in range (1,11) if x % 2 == 0]
print [x^2 for x in range (1,10) if x % 2 == 0]
The start is inclusive and the end is exclusive. This is a general rule
in Python. In fact, there's only one exception that I know of:
randint(a, b) in the 'random' module.
 
G

Gib Bogle

Kit said:
Thank you so much guys.

Just out of curiosity: can I do something like this to "square all
even numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]

Why not try it?
 
S

Steven D'Aprano

Thank you so much guys.

Just out of curiosity: can I do something like this to "square all even
numbers in the range 1-10"?
print [x^2 for x in range (1,11) if x % 2 == 0]

^ is the XOR operator in Python. You want:


[x**2 for x in range (1,11) if x % 2 == 0]

or even better:

[x**2 for x in range (2, 11, 2)]
 
R

Rainer Grimm

Hallo,
you can also look at list comprehension as syntactic sugar for the
functions map and filter. The two functions from the functional world
can be expressed in a comprehensive way with list comprehension.
[x**2 for x in range(10) ] == map ( lambda x: x*x, range(10)) True
[ x for x in range(10) if x%2 == 0 ] == filter ( lambda x: x%2 == 0 , range(10))
True

Greetings
 
L

Luis M. González

Hello Everyone, I am not sure if I have posted this question in a
correct board. Can anyone please teach me:

What is a list compression in Python?

Would you mind give me some list compression examples?

Thanks & really appreciate that.
Kit

It's also worth noting that from Python 3.0 on, you have also dict and
set comprehensions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,177
Messages
2,570,954
Members
47,507
Latest member
codeguru31

Latest Threads

Top