Given a list of 0
to N
sequential integers, where N
is an even number, e.g. A = [0 , 1 , 2 , 3 , 4]
I'm looking for a list comprehension, i.e. of type [a for a in A]
, for creating another list B = [0 , 1 , 1 , 2 , 3 , 3 , 4]
that repeats the odd numbers in the input list, A
.
Asked
Active
Viewed 184 times
1

Olumide
- 5,397
- 10
- 55
- 104
-
That kind of expression is called a list comprehension, but I don't think it can do what you're asking for. – Thomas K Dec 01 '12 at 18:32
-
2It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters Dec 01 '12 at 18:36
-
Try generators! (Hint: `yield` twice.) – Jon Gauthier Dec 01 '12 at 18:36
-
@David I am more of a beginner at Python and I didn't know the name of the type of expression until Thomas K's response (thanks Tom). I can use an explicit for loop but I was wondering if its possible to generate the output list using a list comprehension. – Olumide Dec 01 '12 at 18:39
-
@MartijnPieters don't assume the worst. Please refer to my previous comment. I am just getting to grips with Python and am amazed by how much that can be done in just one line. – Olumide Dec 01 '12 at 18:40
-
1@Olumide, Martijn is pointing out what most everyone on here deals with everyday: people being lazy. I like what David posted about "What have you tried?" as it shows the potential responders what effort you have put in thus far (and what doesn't work). Some answers are VERY lengthy and if you tried some techniques and it didn't work for you, you could potential end up wasting someones valuable time! On that note: since you are new to Python I'd also recommend reading PEP 8 and PEP 20 if you haven't already. I wish someone told me about those when I started out =/ – Justin Carroll Dec 01 '12 at 18:51
-
Okay. I'll note the tradition from now on. Its just that in this case I hadn't the foggiest clue how to proceed with a list comprehension. – Olumide Dec 01 '12 at 19:25
2 Answers
5
Here's a (somewhat ugly) solution with a list comprehension:
a = range(5)
list(itertools.chain(*[[x, x] if x % 2 == 1 else [x] for x in a]))
# => [0, 1, 1, 2, 3, 3, 4]
The list comprehension builds a list of sublists:
[[x, x] if x % 2 == 1 else [x] for x in a] # => [[0], [1, 1], [2], [3, 3], [4]]
The itertools.chain
call is just one of the many ways to flatten a list in Python.

Community
- 1
- 1

Jon Gauthier
- 25,202
- 6
- 63
- 69
-
I came up with something similar, but I couldn't figure out how to "collapse" the list. Rather, in order to output two values, I output it as an array (as you've done). – Alan Dec 01 '12 at 18:43
-
1[Using `itertools.chain`](http://stackoverflow.com/a/953097/815632) is a far more efficient way to chain together lists- concatenating lists becomes very inefficient with large values of N. – David Cain Dec 01 '12 at 18:49
-
-
@David: Wow, 1000% faster on my end. Thanks for enlightening me :) – Jon Gauthier Dec 01 '12 at 18:55
-
2@HansEngel, I'm glad you're enlightened! =) One minor point, though - `itertools.chain` returns a generator, not a list (you might want to clarify this / use `list()` to convert). – David Cain Dec 01 '12 at 19:01