Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list initialization using multiple range statements

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?

like image 931
jackd Avatar asked Dec 19 '12 19:12

jackd


People also ask

How do you initialize a list in Python?

You can initialize a list in Python using square brackets, the list() method, list multiplication, and a list comprehension. Square brackets let you initialize an empty list, or a list which contains some default values. The list() method works in the same way as square brackets.

How do I make a list of 10 elements in Python?

Create Python Lists In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.


3 Answers

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.

like image 125
Óscar López Avatar answered Oct 24 '22 20:10

Óscar López


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)))
like image 45
Shawn Chin Avatar answered Oct 24 '22 19:10

Shawn Chin


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.

like image 28
Silas Ray Avatar answered Oct 24 '22 19:10

Silas Ray