473,388 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Producing multiple items in a list comprehension

Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

....and receive

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

....but of course you really get a list of lists:

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

I'm aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there's some way to avoid the "lists
of lists" generation in the first place?

A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?

Thanks,
---Joel
Jun 27 '08 #1
13 1488
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

...and receive

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

...but of course you really get a list of lists:

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

I'm aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there's some way to avoid the "lists
of lists" generation in the first place?

A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?

Thanks,
---Joel
--
http://mail.python.org/mailman/listinfo/python-list
For the first part:

def gen(n):
for i in xrange(n):
yield i
yield 2*i

print list(gen(4))

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

gerard
Jun 27 '08 #2
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list
of, say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

...and receive

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

...but of course you really get a list of lists:

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

I'm aware I can use any of the standard "flatten" bits of code to turn
this back into what I want, but I was hoping there's some way to avoid the
"lists of lists" generation in the first place?
>>[x*y for x in range(4) for y in 1,2]
[0, 0, 1, 2, 2, 4, 3, 6]

A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip"
to do so?
>>items = [None] * 6
items[::2] = 1,2,3
items[1::2] = 4,5,6
items
[1, 4, 2, 5, 3, 6]

Peter
Jun 27 '08 #3
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

...and receive

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

...but of course you really get a list of lists:

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

I'm aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there's some way to avoid the "lists
of lists" generation in the first place?

A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?

Thanks,
---Joel
--
http://mail.python.org/mailman/listinfo/python-list
For the first part:

def gen(n):
for i in xrange(n):
yield i
yield 2*i

print list(gen(4))

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

gerard

Jun 27 '08 #4

"Joel Koltner" <za***************@yahoo.comwrote in message
news:5R*********************@en-nntp-05.dc1.easynews.com...
Is there an easy way to get a list comprehension to produce a flat list
of, say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

...and receive

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

...but of course you really get a list of lists:

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

I'm aware I can use any of the standard "flatten" bits of code to turn
this back into what I want, but I was hoping there's some way to avoid the
"lists of lists" generation in the first place?

A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip"
to do so?

Thanks,
---Joel
i figured out a solution

sum([x,2*x] for x in range(4)],[]) #not tested
sum(zip(list1,list2),()) #not tested

(you did say you knew of ways to flatten, but i dunno if you knew of that
way or not)

as an aside, i wish that sum didn't have to take the second parameter. it's
kind of superfluous. it can just use the first item as the initial value
instead of 0 when no initial value is specified. it would be a little
complex to do without putting a conditional in your main loop and slowing it
down, but it could be done.
Jun 27 '08 #5
On Thu, 22 May 2008 15:29:42 -0400, inhahe wrote:
"Joel Koltner" <za***************@yahoo.comwrote in message
news:5R*********************@en-nntp-05.dc1.easynews.com...
>Is there an easy way to get a list comprehension to produce a flat list
of, say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

...and receive

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

...but of course you really get a list of lists:

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

I'm aware I can use any of the standard "flatten" bits of code to turn
this back into what I want, but I was hoping there's some way to avoid
the "lists of lists" generation in the first place?

A slightly similar problem: If I want to "merge," say, list1=[1,2,3]
with list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way
with "zip" to do so?

Thanks,
---Joel

i figured out a solution

sum([x,2*x] for x in range(4)],[]) #not tested sum(zip(list1,list2),())
#not tested

(you did say you knew of ways to flatten, but i dunno if you knew of
that way or not)

as an aside, i wish that sum didn't have to take the second parameter.
it's kind of superfluous. it can just use the first item as the initial
value instead of 0 when no initial value is specified. it would be a
little complex to do without putting a conditional in your main loop and
slowing it down, but it could be done.

If you don't want the second parameter use reduce():
>>reduce(operator.add, ([x, x*2] for x in xrange(4)))
[0, 0, 1, 2, 2, 4, 3, 6]
>>reduce(operator.add, zip([1,2,3],[4,5,6]))
(1, 4, 2, 5, 3, 6)

or:
>>reduce(lambda x, y: x.extend(y) or x, ([x, x*2] for x in xrange(4)))
[0, 0, 1, 2, 2, 4, 3, 6]

-- Ivan
Jun 27 '08 #6
Joel Koltner <za***************@yahoo.comwrote:
Is there an easy way to get a list comprehension to produce a flat
list of, say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]

...and receive

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

...but of course you really get a list of lists:

[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm not sure I would recommend it, but try:

[v for x in range(4) for v in (x, 2 * x)]
A slightly similar problem: If I want to "merge," say, list1=[1,2,3]
with list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way
with "zip" to do so?
A similar solution as above should work.

Marc
Jun 27 '08 #7
"Peter Otten" <__*******@web.dewrote in message
news:g1*************@news.t-online.com...
>A slightly similar problem: If I want to "merge," say, list1=[1,2,3] ...
>>>items = [None] * 6
items[::2] = 1,2,3
items[1::2] = 4,5,6
items
[1, 4, 2, 5, 3, 6]
Thanks Peter, that's pretty clean -- I like it!
Jun 27 '08 #8
"inhahe" <in****@gmail.comwrote in message
news:5P*******************@bignews7.bellsouth.net. ..
i figured out a solution

sum([x,2*x] for x in range(4)],[]) #not tested
Nice... thanks; I probably had seen code using 'sum' to flatten but hadn't
actually understood how it worked. After playing around some it's now
clear...

(Add one more opening bracket to your solution to make the interpreter
happy -- I know you know this and it was missed only due to not actually
trying it ought)

---Joel
Jun 27 '08 #9
Hi Marc,

"Marc Christiansen" <us****@solar-empire.dewrote in message
news:39************@pluto.solar-empire.de...
I'm not sure I would recommend it, but try:
[v for x in range(4) for v in (x, 2 * x)]
That certainly works... and it almost seems like a bit less of a hack (if
perhaps somewhat harder to read) than the "sum" approach to flattening?
A similar solution as above should work.
This is what I came up with:
>>list1=[1,2,3]
list2=[4,5,6]
[j for (i,m) in enumerate(list1) for j in (m,list2[i])]
[1, 4, 2, 5, 3, 6]

Not exactly "clean" due to having to enumerate list1 to get the index for
list2. There may be a better method, of course -- so far I like Petter
Otten's approach.

Thanks for the help,
---Joel
Jun 27 '08 #10
Peter Otten <__*******@web.dewrote:
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip"
to do so?
>items = [None] * 6
items[::2] = 1,2,3
items[1::2] = 4,5,6
items
[1, 4, 2, 5, 3, 6]
My problem with this solution is that you depend on knowing how many
elements you have in each list ahead of time. Assuming that both list
are of the same length, then, I find the following more elegant:

list1=[1,2,3]
list2=[4,5,6]

reduce(lambda x, y: x+y, zip(list1, list2))

of course, zip creates tuples, so you end up with a tuple, therefore if
you need for your solution to be a list:
list(reduce(lambda x, y: x+y, zip(list1, list2)))
of
reduce(lambda x, y: x+y, list(zip(list1, list2)) )
Yves.
http://www.SollerS.ca

Jun 27 '08 #11
Yves Dorfsman wrote:
Peter Otten <__*******@web.dewrote:
A slightly similar problem: If I want to "merge," say, list1=[1,2,3]
with list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way
with "zip" to do so?
>>items = [None] * 6
items[::2] = 1,2,3
items[1::2] = 4,5,6
items
[1, 4, 2, 5, 3, 6]

My problem with this solution is that you depend on knowing how many
elements you have in each list ahead of time.
$ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100; from operator import
add" "list(reduce(add, zip(a, b)))"
1000 loops, best of 3: 637 usec per loop

$ python -m timeit -s"a = [1,2,3]*100; b =
[4,5,6]*100" "items=[None]*(2*len(a)); items[::2] = a; items[1::2] = b"
10000 loops, best of 3: 23.4 usec per loop

The speed gain is significant. Why should I throw away useful information if
I have it? I'd even be willing to convert one arbitrary iterable to a list
to get the length information.

$ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100" "aa = list(a);
items=[None]*(2*len(aa)); items[::2] = aa; items[1::2] = b"
10000 loops, best of 3: 29.5 usec per loop
Assuming that both list
are of the same length, then, I find the following more elegant:

list1=[1,2,3]
list2=[4,5,6]

reduce(lambda x, y: x+y, zip(list1, list2))

of course, zip creates tuples, so you end up with a tuple, therefore if
you need for your solution to be a list:
list(reduce(lambda x, y: x+y, zip(list1, list2)))
I'd rather use a plain old for-loop:
>>from itertools import izip
a = [1,2,3]
b = [4,5,6]
items = []
for chunk in izip(a, b):
.... items.extend(chunk)
....
>>items
[1, 4, 2, 5, 3, 6]

$ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100; from itertools
import izip" "items = []
for chunk in izip(a, b): items.extend(chunk)"
1000 loops, best of 3: 242 usec per loop

$ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100; from itertools
import izip" "items = []; extend = items.extend
for chunk in izip(a, b): extend(chunk)"
10000 loops, best of 3: 70.9 usec per loop
reduce(lambda x, y: x+y, list(zip(list1, list2)) )
list(zip(...)) has no effect here. Possible fixes
>>reduce(lambda x, y: x + list(y), zip(list1, list2), [])
[1, 4, 2, 5, 3, 6]
>>reduce(lambda x, y: x.extend(y) or x, zip(list1, list2), [])
[1, 4, 2, 5, 3, 6]

$ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100" "reduce(lambda x, y:
x.extend(y) or x, zip(a, b), [])"
1000 loops, best of 3: 368 usec per loop

are more complicated than elegant. Not recommended.

Peter
Jun 27 '08 #12
Peter Otten <__*******@web.dewrote:
The speed gain is significant. Why should I throw away useful information if
I have it?
My thinking was that it wasn't generic enough, and I was looking for a
solution that would work for more generic problem. I agree, I shouldn't
have used the world "elegant" here, more generic would have been better.
I'd even be willing to convert one arbitrary iterable to a list
to get the length information.
$ python -m timeit -s"a = [1,2,3]*100; b = [4,5,6]*100" "aa = list(a);
items=[None]*(2*len(aa)); items[::2] = aa; items[1::2] = b"
10000 loops, best of 3: 29.5 usec per loop
Yes, I like that, it is as generic as the reduce one.

are more complicated than elegant. Not recommended.
You proved your point. Every book and webpage out there says that
functional solutions are faster than loops, that's why I tried hard not
to use a loop, and "reduce" is considered functional - I should have
timed it.
Yves.
http://www.SollerS.ca

Jun 27 '08 #13
On May 22, 7:21 pm, "Joel Koltner" <zapwireDASHgro...@yahoo.com>
wrote:
Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?

E.g., I'd like to do something like:

[ [x,2*x] for x in range(4) ]
[x * i for x in xrange(4) for i in xrange(1, 3)]

--
Paul Hankin
Jun 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
2
by: Peter | last post by:
Hello! Please, could anyone tell, is it possible to set multiple items to be selected in list control in the code? For example when the web form is loaded three items of 5 are selected in list...
7
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
1
NeoPa
by: NeoPa | last post by:
A number of posters have asked to be shown how to produce a list of items from multiple records which are (potentially) grouped together. Take the following data for instance (from a table called )...
3
by: pi.arctan | last post by:
Hi guys, One of my many project involves working with YUV-files, where I need to reduce the vertical resolution with a factor of two, i.e. remove every other scan line. Today I'm using two...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.