Velocity Reviews > list comprehension question

# list comprehension question

Ross
Guest
Posts: n/a

 05-01-2009
If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
return a new list of each individual element in these tuples, I can do
it with a nested for loop but when I try to do it using the list
comprehension b = [j for j in i for i in a], my output is b =
[5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
doing wrong?

Chris Rebert
Guest
Posts: n/a

 05-01-2009
On Thu, Apr 30, 2009 at 5:56 PM, Ross <(E-Mail Removed)> wrote:
> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
> return a new list of each individual element in these tuples, I can do
> it with a nested for loop but when I try to do it using the list
> comprehension b = [j for j in i for i in a], my output is b =
> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
> doing wrong?

Your comprehension is the identity comprehension (i.e. it effectively
just copies the list as-is).
What you're trying to do is difficult if not impossible to do as a
comprehension.

Here's another approach:
b = list(itertools.chain.from_iterable(a))

And without using a library function:
b = []
for pair in a:
for item in pair:
b.append(item)

Cheers,
Chris
--
http://blog.rebertia.com

Michael Spencer
Guest
Posts: n/a

 05-01-2009
Ross wrote:
> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
> return a new list of each individual element in these tuples, I can do
> it with a nested for loop but when I try to do it using the list
> comprehension b = [j for j in i for i in a], my output is b =
> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
> doing wrong?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

>>> a = [(1,2), (3,4), (5,6)]
>>> [i for t in a for i in t]

[1, 2, 3, 4, 5, 6]

Or, with different spacing to make the nesting clearer:

>>> [i

... for t in a
... for i in t]
[1, 2, 3, 4, 5, 6]
>>>

Michael

Arnaud Delobelle
Guest
Posts: n/a

 05-01-2009
Ross <(E-Mail Removed)> writes:

> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
> return a new list of each individual element in these tuples, I can do
> it with a nested for loop but when I try to do it using the list
> comprehension b = [j for j in i for i in a], my output is b =
> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
> doing wrong?

When writing nested list comprehension, the for loops are in the same
order as you would write a normal nested for loop (which is not
necessarily intuitive when you first find out but is very handy in the
long run I think).

So write:

[j for i in a for j in i]

--
Arnaud

thor
Guest
Posts: n/a

 05-01-2009
On May 1, 2:28*pm, Arnaud Delobelle <(E-Mail Removed)> wrote:
> Ross <(E-Mail Removed)> writes:
> > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
> > return a new list of each individual element in these tuples, I can do
> > it with a nested for loop but when I try to do it using the list
> > comprehension b = [j for j in i for i in a], my output is b =
> > [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
> > doing wrong?

>
> When writing nested list comprehension, the for loops are in the same
> order as you would write a normal nested for loop (which is not
> necessarily intuitive when you first find out but is very handy in the
> long run I think).
>
> So write:
>
> * * [j for i in a for j in i]
>
> --
> Arnaud

an trick
>>> a

[(1, 2), (3, 4), (5, 6)]
>>> sum(a, ())

(1, 2, 3, 4, 5, 6)
>>>

you may search the maillist , somebody questioned before

J Kenneth King
Guest
Posts: n/a

 05-01-2009
Chris Rebert <(E-Mail Removed)> writes:

> On Thu, Apr 30, 2009 at 5:56 PM, Ross <(E-Mail Removed)> wrote:
>> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
>> return a new list of each individual element in these tuples, I can do
>> it with a nested for loop but when I try to do it using the list
>> comprehension b = [j for j in i for i in a], my output is b =
>> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
>> doing wrong?

>
> Your comprehension is the identity comprehension (i.e. it effectively
> just copies the list as-is).
> What you're trying to do is difficult if not impossible to do as a
> comprehension.
>
> Here's another approach:
> b = list(itertools.chain.from_iterable(a))
>
> And without using a library function:
> b = []
> for pair in a:
> for item in pair:
> b.append(item)

This is much more clear than a nested comprehension.

I love comprehensions, but abusing them can lead to really dense and

Emile van Sebille
Guest
Posts: n/a

 05-01-2009
On 5/1/2009 7:31 AM J Kenneth King said...
> Chris Rebert <(E-Mail Removed)> writes:
>> b = []
>> for pair in a:
>> for item in pair:
>> b.append(item)

>
> This is much more clear than a nested comprehension.
>
> I love comprehensions, but abusing them can lead to really dense and

I disagree on dense and difficult, although I'll leave open the question
of abuse.

b = [ item for pair in a for item in pair ]

This is exactly the code above expressed in comprehension form.

It's worth knowing that a list comprehension is structured identically
to the equivalent for loop. So it really is neither more dense nor more
difficult to read. Further, you can tell immediately from the start of
the list comprehension what you've got -- in this case a list of item(s).

Here with some slight changes...

>>> a = [(1, 2), (3, 4, 7), (5, 6)]
>>> [ item for j in a if len(j)==2 for item in j if item % 2 ]

[1, 5]

....opposed to...

>>> for j in a:

.... if len(j)==2:
.... for item in j:
.... if item % 2:
.... b.append(item)
....
>>> b

[1, 5]
>>>

YMMV,

Emile

Arnaud Delobelle
Guest
Posts: n/a

 05-01-2009
Emile van Sebille <(E-Mail Removed)> writes:

> On 5/1/2009 7:31 AM J Kenneth King said...
>> Chris Rebert <(E-Mail Removed)> writes:
>>> b = []
>>> for pair in a:
>>> for item in pair:
>>> b.append(item)

>>
>> This is much more clear than a nested comprehension.
>>
>> I love comprehensions, but abusing them can lead to really dense and

>
> I disagree on dense and difficult, although I'll leave open the
> question of abuse.
>
> b = [ item for pair in a for item in pair ]
>
> This is exactly the code above expressed in comprehension form.

If the comprehension above is an abuse, then every nested list
comprehension is an abuse of comprehensions so they might as well not be
in the language...

--
Arnaud

Shane Geiger
Guest
Posts: n/a

 05-01-2009
from goopy.functional import flatten #
http://sourceforge.net/projects/goog-goopy/
b = [(1,2), (3,4), (5,6)]
print flatten(b)

#from goopy.functional import flatten #
http://sourceforge.net/projects/goog-goopy/

def flatten(seq):
"""
Returns a list of the contents of seq with sublists and tuples "exploded".
The resulting list does not contain any sequences, and all inner sequences
are exploded. For example:

>>> flatten([7,(6,[5,4],3),2,1])

[7,6,5,4,3,2,1]
"""
lst = []
for el in seq:
if type(el) == list or type(el) is tuple:
lst.extend(flatten(el))
else:
lst.append(el)
return lst

Chris Rebert wrote:
> On Thu, Apr 30, 2009 at 5:56 PM, Ross <(E-Mail Removed)> wrote:
>
>> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
>> return a new list of each individual element in these tuples, I can do
>> it with a nested for loop but when I try to do it using the list
>> comprehension b = [j for j in i for i in a], my output is b =
>> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
>> doing wrong?
>>

>
> Your comprehension is the identity comprehension (i.e. it effectively
> just copies the list as-is).
> What you're trying to do is difficult if not impossible to do as a
> comprehension.
>
> Here's another approach:
> b = list(itertools.chain.from_iterable(a))
>
> And without using a library function:
> b = []
> for pair in a:
> for item in pair:
> b.append(item)
>
> Cheers,
> Chris
>

--
Shane Geiger, IT Director
Council For Economic Education / www.councilforeconed.org
http://www.velocityreviews.com/forums/(E-Mail Removed) / 402-438-8958

Teaching Opportunity

John Posner
Guest
Posts: n/a

 05-01-2009
Shane Geiger wrote:
> if type(el) == list or type(el) is tuple:

A tiny improvement:

if type(el) in (list, tuple):