quick beginners List comprehension question


D

Dr Mephesto

Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension? Thanks in Advance!
 
Ad

Advertisements

D

Diez B. Roggisch

Dr said:
Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension? Thanks in Advance!

No, as such, because list-comprehensions require you to have an *expression*
in front of the iteration:

resultlist = [<expr> for <variable(s)> in <iterable>]

Now what you of course can do is this:

def multiply(item):
item.bar = item.bar * 3

[multiply(i) for i in items]

However, doing this will make python produce a list of None-references -
which is a waste. It's up to you if you care about that, but generally it
is frowned upon because of that, and the fact that the conciseness of the
list-comp here isn't really helping with the readability.

Diez
 
M

MRAB

Dr said:
Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension? Thanks in Advance!
You could reduce that to:

for x in Newlist:
x.bar *= 3

but I don't think you could do it with list comprehension.
 
P

Philip Semanchuk

Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension? Thanks in Advance!

Other answers have been good; to them I'll add the comment that list
comprehensions are for *constructing* lists, not manipulating the
elements thereof.

HTH
Philip
 
M

MRAB

Diez said:
Dr said:
Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension? Thanks in Advance!

No, as such, because list-comprehensions require you to have an *expression*
in front of the iteration:

resultlist = [<expr> for <variable(s)> in <iterable>]

Now what you of course can do is this:

def multiply(item):
item.bar = item.bar * 3

[multiply(i) for i in items]

However, doing this will make python produce a list of None-references -
which is a waste. It's up to you if you care about that, but generally it
is frowned upon because of that, and the fact that the conciseness of the
list-comp here isn't really helping with the readability.
If you had:

def multiply(item):
item.bar = item.bar * 3
return item

then:

[multiply(i) for i in items]

would return items. Still a bad idea, though, because you're using a
list comprehension for its side-effect.
 
L

Lou Pecora

Philip Semanchuk said:
Other answers have been good; to them I'll add the comment that list
comprehensions are for *constructing* lists, not manipulating the
elements thereof.

HTH
Philip


Well this seems to work just fine. What am I missing:

A=[1,2,3]
print A
A=[2*a for a in A]
print A
 
Ad

Advertisements

S

Steve Holden

Lou said:
Philip Semanchuk said:
Other answers have been good; to them I'll add the comment that list
comprehensions are for *constructing* lists, not manipulating the
elements thereof.

HTH
Philip


Well this seems to work just fine. What am I missing:

A=[1,2,3]
print A
A=[2*a for a in A]
print A
The fact that the lists to be multiplied are attributes of a list of
objects, and therefore aren't themselves a list. Look more closely at
the original poster's question.

regards
Steve
 
P

Philip Semanchuk

Philip Semanchuk said:
Other answers have been good; to them I'll add the comment that list
comprehensions are for *constructing* lists, not manipulating the
elements thereof.

HTH
Philip


Well this seems to work just fine. What am I missing:

A=[1,2,3]
print A
A=[2*a for a in A]
print A

You haven't manipulated the list A, you've simply overwritten it with
a new list.
 
T

Terry Reedy

Dr said:
Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Constructing this list is the appropriate place for a comprehension.
Newlist = [Foo() for _ in range(10)]
Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

Use MRAB's replacement for this.
but can I do this using list comprehension?

Don't, for reasons given by others.

tjr
 
Ad

Advertisements

D

Diez B. Roggisch

MRAB said:
Diez said:
Dr said:
Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension? Thanks in Advance!

No, as such, because list-comprehensions require you to have an
*expression*
in front of the iteration:

resultlist = [<expr> for <variable(s)> in <iterable>]

Now what you of course can do is this:

def multiply(item):
item.bar = item.bar * 3

[multiply(i) for i in items]

However, doing this will make python produce a list of None-references -
which is a waste. It's up to you if you care about that, but generally it
is frowned upon because of that, and the fact that the conciseness of the
list-comp here isn't really helping with the readability.
If you had:

def multiply(item):
item.bar = item.bar * 3
return item

then:

[multiply(i) for i in items]

would return items. Still a bad idea, though, because you're using a
list comprehension for its side-effect.

And redundant, which was the reason I ommited it.

Diez
 

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

Top