19

I want one long list, say [1,2,3,4,5,15,16,17,18,19] as an example. To initialize this, I try typing:

new_list = [range(1,6),range(15,20)]

However this doesn't do what I want, returning:

[[1, 2, 3, 4, 5], [15, 16, 17, 18, 19]]

When I do:

len(new_list)

It returns 2, instead of the 10 elements I wanted (since it made 2 lists inside the list). Obviously in this example I could just type out what I want, but I'm trying to do this for some odd iterated lists that go like:

new_list = [range(101,6284),8001,8003,8010,range(10000,12322)]

Desiring a 1-D list instead of a list of lists (or whatever it's best called). I'm guessing this is really easy and I'm missing it, but after quite a bit of searching I've come up with nothing too useful. Any ideas?

jackd
  • 337
  • 3
  • 6
  • 14

8 Answers8

36

Try this for Python 2.x:

 range(1,6) + range(15,20)

Or if you're using Python3.x, try this:

list(range(1,6)) + list(range(15,20))

For dealing with elements in-between, for Python 2.x:

range(101,6284) + [8001,8003,8010] + range(10000,12322)

And finally for dealing with elements in-between, for Python 3.x:

list(range(101,6284)) + [8001,8003,8010] + list(range(10000,12322))

The key aspects to remember here is that in Python 2.x range returns a list and in Python 3.x it returns an iterable (so it needs to be explicitly converted to a list). And that for appending together lists, you can use the + operator.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Is there any easy way to insert single numbers in between, all in one line? I'm guessing I could append each one, but there's quite few sporadically spaced between list statements, thanks for the help. – jackd Dec 19 '12 at 19:25
  • @jackd updated my answer. Just pack them in a single-element list, like so: `range(1,3) + [3] + range(4,6)` – Óscar López Dec 19 '12 at 19:27
  • You already got it, I was just too slow to notice, thanks this is exactly what I needed. Simple, just couldn't find the right keyword search to land on the answer! – jackd Dec 19 '12 at 19:27
9

You can use itertools.chain to flatten the output of your range() calls.

import itertools
new_list = list(itertools.chain(xrange(1,6), xrange(15,20)))

Using xrange (or simply range() for python3) to get an iterable and chaining them together means only one list object gets created (no intermediate lists required).

If you need to insert intermediate values, just include a list/tuple in the chain:

new_list = list(itertools.chain((-3,-1), 
                                xrange(1,6), 
                                tuple(7),  # make single element iterable
                                xrange(15,20)))
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • 1
    To include a single value, make sure it's a single item in an iterable, e.g. `tuple(8001)` or `[8001]` or `list(8001)`, otherwise it will add `8`, `0`, `0`, and `1`. – kreativitea Dec 19 '12 at 20:49
3

range returns a list to begin with, so you need to either concatenate them together with + or use append() or extend().

new_list = range(1,6) + range(15,20)

or

new_list = range(101,6284)
mew_list.extend([8001,8003,8010])
mew_list.extend(range(10000,12322))

Alternatively, you could use itertools.chain() as shown in Shawn Chin's answer.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
3

Just unpack the range values as follows:

new_list = [*range(1,6), *range(15,20)]

print(new_list)
# [1, 2, 3, 4, 5, 15, 16, 17, 18, 19]

print(len(new_list))
# 10

print(type(new_list))
# <class 'list'>
fcdt
  • 2,371
  • 5
  • 14
  • 26
1

Try this:

from itertools import chain

new_list = [x for x in chain(range(1,6), range(15,20))]
print new_list

Output like you wanted:

[1, 2, 3, 4, 5, 15, 16, 17, 18, 19]
jackcogdill
  • 4,900
  • 3
  • 30
  • 48
0

i would like to propose u a version without +

import operator
a = list(range(1,6))
b = list(range(7,9))
print(operator.concat(a,b))
Mikhail
  • 2,690
  • 4
  • 28
  • 43
0

Just unpack range values as follows:

>>> new_list = [*range(1,6),*range(15,20)]
>>> new_list
[1, 2, 3, 4, 5, 15, 16, 17, 18, 19]
>>> len(new_list)
10
remram
  • 4,805
  • 1
  • 29
  • 42
  • 1
    Please don't post another answer to update your existing answer. Just use the [edit] button. After you have already posted [a new answer](https://stackoverflow.com/a/63873825/13865476), I would recommend deleting this one. – fcdt Sep 13 '20 at 22:04
0

you can use itertools.chain function and unpacking operator (*) for it

Example 1:

from itertools import chain

output = [*chain(range(1, 6), range(15, 20))]

Example 2:

from itertools import chain

data = [range(1, 6), range(15, 20)]
output = [*chain(*data)]

Example 3 using chain.fromiterable:

data = [range(1, 6), range(15, 20)]
output = [*chain.from_iterable(data)]

output

assert output == [1, 2, 3, 4, 5, 15, 16, 17, 18, 19]
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179