Define a list range inside a list The 2019 Stack Overflow Developer Survey Results Are In ...

One-dimensional Japanese puzzle

Working through the single responsibility principle (SRP) in Python when calls are expensive

Could an empire control the whole planet with today's comunication methods?

Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?

how can a perfect fourth interval be considered either consonant or dissonant?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

What aspect of planet Earth must be changed to prevent the industrial revolution?

Presidential Pardon

"is" operation returns false even though two objects have same id

Can each chord in a progression create its own key?

Huge performance difference of the command find with and without using %M option to show permissions

Is an up-to-date browser secure on an out-of-date OS?

Drawing vertical/oblique lines in Metrical tree (tikz-qtree, tipa)

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Is 'stolen' appropriate word?

Didn't get enough time to take a Coding Test - what to do now?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Homework question about an engine pulling a train

Button changing its text & action. Good or terrible?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

Is there a writing software that you can sort scenes like slides in PowerPoint?

Are spiders unable to hurt humans, especially very small spiders?

How to determine omitted units in a publication



Define a list range inside a list



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow can I generate a list of consecutive numbers?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to return multiple values from a function?How to make a flat list out of list of lists“Least Astonishment” and the Mutable Default Argumentlist comprehension vs. lambda + filterHow do I list all files of a directory?Fastest way to check if a value exist in a listWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







9















in python is there a way to create of list that will skip numbers and will continue after skipping? something like the following code:



x = [1...3, 6...10]
print(x)
# [1,2,3,6,7,8,9,10]


Well its easy to write a for loop and then skip each defined index/value, or i can just use range, what I am looking for is a shorter more readable line. If not I can understand.










share|improve this question

























  • Possible duplicate of How can I generate a list of consecutive numbers?

    – ivan_pozdeev
    9 hours ago






  • 3





    No @ivan_pozdeev, the suggested dupe does not consider multiple slice objects, rather a simple continuous range

    – yatu
    9 hours ago








  • 1





    Python doesn't support any special range syntax, never mind implicit concatenation of such within the same list literal.

    – chepner
    2 hours ago


















9















in python is there a way to create of list that will skip numbers and will continue after skipping? something like the following code:



x = [1...3, 6...10]
print(x)
# [1,2,3,6,7,8,9,10]


Well its easy to write a for loop and then skip each defined index/value, or i can just use range, what I am looking for is a shorter more readable line. If not I can understand.










share|improve this question

























  • Possible duplicate of How can I generate a list of consecutive numbers?

    – ivan_pozdeev
    9 hours ago






  • 3





    No @ivan_pozdeev, the suggested dupe does not consider multiple slice objects, rather a simple continuous range

    – yatu
    9 hours ago








  • 1





    Python doesn't support any special range syntax, never mind implicit concatenation of such within the same list literal.

    – chepner
    2 hours ago














9












9








9








in python is there a way to create of list that will skip numbers and will continue after skipping? something like the following code:



x = [1...3, 6...10]
print(x)
# [1,2,3,6,7,8,9,10]


Well its easy to write a for loop and then skip each defined index/value, or i can just use range, what I am looking for is a shorter more readable line. If not I can understand.










share|improve this question
















in python is there a way to create of list that will skip numbers and will continue after skipping? something like the following code:



x = [1...3, 6...10]
print(x)
# [1,2,3,6,7,8,9,10]


Well its easy to write a for loop and then skip each defined index/value, or i can just use range, what I am looking for is a shorter more readable line. If not I can understand.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 hours ago







Led

















asked 9 hours ago









LedLed

424515




424515













  • Possible duplicate of How can I generate a list of consecutive numbers?

    – ivan_pozdeev
    9 hours ago






  • 3





    No @ivan_pozdeev, the suggested dupe does not consider multiple slice objects, rather a simple continuous range

    – yatu
    9 hours ago








  • 1





    Python doesn't support any special range syntax, never mind implicit concatenation of such within the same list literal.

    – chepner
    2 hours ago



















  • Possible duplicate of How can I generate a list of consecutive numbers?

    – ivan_pozdeev
    9 hours ago






  • 3





    No @ivan_pozdeev, the suggested dupe does not consider multiple slice objects, rather a simple continuous range

    – yatu
    9 hours ago








  • 1





    Python doesn't support any special range syntax, never mind implicit concatenation of such within the same list literal.

    – chepner
    2 hours ago

















Possible duplicate of How can I generate a list of consecutive numbers?

– ivan_pozdeev
9 hours ago





Possible duplicate of How can I generate a list of consecutive numbers?

– ivan_pozdeev
9 hours ago




3




3





No @ivan_pozdeev, the suggested dupe does not consider multiple slice objects, rather a simple continuous range

– yatu
9 hours ago







No @ivan_pozdeev, the suggested dupe does not consider multiple slice objects, rather a simple continuous range

– yatu
9 hours ago






1




1





Python doesn't support any special range syntax, never mind implicit concatenation of such within the same list literal.

– chepner
2 hours ago





Python doesn't support any special range syntax, never mind implicit concatenation of such within the same list literal.

– chepner
2 hours ago












4 Answers
4






active

oldest

votes


















13














Simplest way to do this is to call range() and unpack result inside list assignment.



x = [*range(1, 4), *range(6, 11)]





share|improve this answer


























  • Please name and refer to the docs of the corresponding syntax constructs

    – ivan_pozdeev
    9 hours ago











  • whats with the *?

    – Led
    9 hours ago











  • Exactly this is the way to go. gj

    – Netwave
    9 hours ago






  • 2





    @Led, Unpacking arguments

    – Olvin Roght
    9 hours ago






  • 1





    While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

    – marcelm
    6 hours ago



















11














Alternatively you can use itertools.chain:



>>> import itertools
>>> list(itertools.chain(range(1, 5), range(20, 25)))
[1, 2, 3, 4, 20, 21, 22, 23, 24]





share|improve this answer
























  • interesting, but is there no native way? i mean I need to import something? thank you very much

    – Led
    9 hours ago






  • 4





    @Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

    – Netwave
    9 hours ago






  • 2





    @Led why does it matter if you have to import from the standard library? It's part of Python

    – juanpa.arrivillaga
    9 hours ago











  • im looking for something short without importing.

    – Led
    9 hours ago






  • 3





    @Led Importing is your friend. Learn that sooner rather than later.

    – wizzwizz4
    6 hours ago



















5














If numpy is an option, you can use np.r_ to concatenate slice objects:



import numpy as np
np.r_[range(1,4), range(6,11)]
# array([ 1, 2, 3, 6, 7, 8, 9, 10])





share|improve this answer
























  • And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

    – miradulo
    2 hours ago



















2














You can turn it into a recursive function:



def recursive_ranges(ranges):
if len(ranges) == 1:
return list(range(*ranges[0]))
else:
return list(range(*ranges[0])) + recursive_ranges(ranges[1:])


You can then call this, specifying ranges as a list of lists:



ranges = [[1, 4], [6, 11]]
recursive_ranges(ranges)
# [1, 2, 3, 6, 7, 8, 9, 10]


Note the *ranges[0] is used to unpack the elements in ranges[0] into individual arguments. Essentially the recursive function keeps grabbing the first element of ranges, each element of which is a two-element array, and passing those numbers into the range() method as two different values instead of one array. That's what the * does, it unpacks the array. First call, you unpack [1, 4] into range(1, 4) and then append the next call of the recursive function to it.



Basically this unpacks into the following:



list(range(1, 4)) + list(range(6, 11))


but you get to use a much more compact syntax, just passing a list of lists.






share|improve this answer





















  • 1





    this will be too long for larger numbers

    – Led
    9 hours ago






  • 1





    @Led what do you mean? Also see my update with the recursive function. Pretty compact.

    – Engineero
    9 hours ago













  • thanks, but there is a need to write a function. Great idea by the way

    – Led
    9 hours ago












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55654889%2fdefine-a-list-range-inside-a-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









13














Simplest way to do this is to call range() and unpack result inside list assignment.



x = [*range(1, 4), *range(6, 11)]





share|improve this answer


























  • Please name and refer to the docs of the corresponding syntax constructs

    – ivan_pozdeev
    9 hours ago











  • whats with the *?

    – Led
    9 hours ago











  • Exactly this is the way to go. gj

    – Netwave
    9 hours ago






  • 2





    @Led, Unpacking arguments

    – Olvin Roght
    9 hours ago






  • 1





    While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

    – marcelm
    6 hours ago
















13














Simplest way to do this is to call range() and unpack result inside list assignment.



x = [*range(1, 4), *range(6, 11)]





share|improve this answer


























  • Please name and refer to the docs of the corresponding syntax constructs

    – ivan_pozdeev
    9 hours ago











  • whats with the *?

    – Led
    9 hours ago











  • Exactly this is the way to go. gj

    – Netwave
    9 hours ago






  • 2





    @Led, Unpacking arguments

    – Olvin Roght
    9 hours ago






  • 1





    While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

    – marcelm
    6 hours ago














13












13








13







Simplest way to do this is to call range() and unpack result inside list assignment.



x = [*range(1, 4), *range(6, 11)]





share|improve this answer















Simplest way to do this is to call range() and unpack result inside list assignment.



x = [*range(1, 4), *range(6, 11)]






share|improve this answer














share|improve this answer



share|improve this answer








edited 9 hours ago

























answered 9 hours ago









Olvin RoghtOlvin Roght

34219




34219













  • Please name and refer to the docs of the corresponding syntax constructs

    – ivan_pozdeev
    9 hours ago











  • whats with the *?

    – Led
    9 hours ago











  • Exactly this is the way to go. gj

    – Netwave
    9 hours ago






  • 2





    @Led, Unpacking arguments

    – Olvin Roght
    9 hours ago






  • 1





    While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

    – marcelm
    6 hours ago



















  • Please name and refer to the docs of the corresponding syntax constructs

    – ivan_pozdeev
    9 hours ago











  • whats with the *?

    – Led
    9 hours ago











  • Exactly this is the way to go. gj

    – Netwave
    9 hours ago






  • 2





    @Led, Unpacking arguments

    – Olvin Roght
    9 hours ago






  • 1





    While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

    – marcelm
    6 hours ago

















Please name and refer to the docs of the corresponding syntax constructs

– ivan_pozdeev
9 hours ago





Please name and refer to the docs of the corresponding syntax constructs

– ivan_pozdeev
9 hours ago













whats with the *?

– Led
9 hours ago





whats with the *?

– Led
9 hours ago













Exactly this is the way to go. gj

– Netwave
9 hours ago





Exactly this is the way to go. gj

– Netwave
9 hours ago




2




2





@Led, Unpacking arguments

– Olvin Roght
9 hours ago





@Led, Unpacking arguments

– Olvin Roght
9 hours ago




1




1





While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

– marcelm
6 hours ago





While this works, it's worth noting that this creates the entire list in memory. If you just want to iterate over the resulting sequence once, then this will consume more memory than the itertools.chain method. 40 bytes per number on my system, to be exact. This is fine for small sequences, but if your sequence has a billion numbers, well, you do the math...

– marcelm
6 hours ago













11














Alternatively you can use itertools.chain:



>>> import itertools
>>> list(itertools.chain(range(1, 5), range(20, 25)))
[1, 2, 3, 4, 20, 21, 22, 23, 24]





share|improve this answer
























  • interesting, but is there no native way? i mean I need to import something? thank you very much

    – Led
    9 hours ago






  • 4





    @Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

    – Netwave
    9 hours ago






  • 2





    @Led why does it matter if you have to import from the standard library? It's part of Python

    – juanpa.arrivillaga
    9 hours ago











  • im looking for something short without importing.

    – Led
    9 hours ago






  • 3





    @Led Importing is your friend. Learn that sooner rather than later.

    – wizzwizz4
    6 hours ago
















11














Alternatively you can use itertools.chain:



>>> import itertools
>>> list(itertools.chain(range(1, 5), range(20, 25)))
[1, 2, 3, 4, 20, 21, 22, 23, 24]





share|improve this answer
























  • interesting, but is there no native way? i mean I need to import something? thank you very much

    – Led
    9 hours ago






  • 4





    @Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

    – Netwave
    9 hours ago






  • 2





    @Led why does it matter if you have to import from the standard library? It's part of Python

    – juanpa.arrivillaga
    9 hours ago











  • im looking for something short without importing.

    – Led
    9 hours ago






  • 3





    @Led Importing is your friend. Learn that sooner rather than later.

    – wizzwizz4
    6 hours ago














11












11








11







Alternatively you can use itertools.chain:



>>> import itertools
>>> list(itertools.chain(range(1, 5), range(20, 25)))
[1, 2, 3, 4, 20, 21, 22, 23, 24]





share|improve this answer













Alternatively you can use itertools.chain:



>>> import itertools
>>> list(itertools.chain(range(1, 5), range(20, 25)))
[1, 2, 3, 4, 20, 21, 22, 23, 24]






share|improve this answer












share|improve this answer



share|improve this answer










answered 9 hours ago









NetwaveNetwave

14.1k22247




14.1k22247













  • interesting, but is there no native way? i mean I need to import something? thank you very much

    – Led
    9 hours ago






  • 4





    @Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

    – Netwave
    9 hours ago






  • 2





    @Led why does it matter if you have to import from the standard library? It's part of Python

    – juanpa.arrivillaga
    9 hours ago











  • im looking for something short without importing.

    – Led
    9 hours ago






  • 3





    @Led Importing is your friend. Learn that sooner rather than later.

    – wizzwizz4
    6 hours ago



















  • interesting, but is there no native way? i mean I need to import something? thank you very much

    – Led
    9 hours ago






  • 4





    @Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

    – Netwave
    9 hours ago






  • 2





    @Led why does it matter if you have to import from the standard library? It's part of Python

    – juanpa.arrivillaga
    9 hours ago











  • im looking for something short without importing.

    – Led
    9 hours ago






  • 3





    @Led Importing is your friend. Learn that sooner rather than later.

    – wizzwizz4
    6 hours ago

















interesting, but is there no native way? i mean I need to import something? thank you very much

– Led
9 hours ago





interesting, but is there no native way? i mean I need to import something? thank you very much

– Led
9 hours ago




4




4





@Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

– Netwave
9 hours ago





@Led, itertools is one of the most powerful libs that come with python, it is as native as it can be. Otherwise the unpacking sintax is handy also.

– Netwave
9 hours ago




2




2





@Led why does it matter if you have to import from the standard library? It's part of Python

– juanpa.arrivillaga
9 hours ago





@Led why does it matter if you have to import from the standard library? It's part of Python

– juanpa.arrivillaga
9 hours ago













im looking for something short without importing.

– Led
9 hours ago





im looking for something short without importing.

– Led
9 hours ago




3




3





@Led Importing is your friend. Learn that sooner rather than later.

– wizzwizz4
6 hours ago





@Led Importing is your friend. Learn that sooner rather than later.

– wizzwizz4
6 hours ago











5














If numpy is an option, you can use np.r_ to concatenate slice objects:



import numpy as np
np.r_[range(1,4), range(6,11)]
# array([ 1, 2, 3, 6, 7, 8, 9, 10])





share|improve this answer
























  • And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

    – miradulo
    2 hours ago
















5














If numpy is an option, you can use np.r_ to concatenate slice objects:



import numpy as np
np.r_[range(1,4), range(6,11)]
# array([ 1, 2, 3, 6, 7, 8, 9, 10])





share|improve this answer
























  • And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

    – miradulo
    2 hours ago














5












5








5







If numpy is an option, you can use np.r_ to concatenate slice objects:



import numpy as np
np.r_[range(1,4), range(6,11)]
# array([ 1, 2, 3, 6, 7, 8, 9, 10])





share|improve this answer













If numpy is an option, you can use np.r_ to concatenate slice objects:



import numpy as np
np.r_[range(1,4), range(6,11)]
# array([ 1, 2, 3, 6, 7, 8, 9, 10])






share|improve this answer












share|improve this answer



share|improve this answer










answered 9 hours ago









yatuyatu

15.8k41642




15.8k41642













  • And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

    – miradulo
    2 hours ago



















  • And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

    – miradulo
    2 hours ago

















And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

– miradulo
2 hours ago





And since we just need slice objects you can write np.r_[1:4, 6:11] as well.

– miradulo
2 hours ago











2














You can turn it into a recursive function:



def recursive_ranges(ranges):
if len(ranges) == 1:
return list(range(*ranges[0]))
else:
return list(range(*ranges[0])) + recursive_ranges(ranges[1:])


You can then call this, specifying ranges as a list of lists:



ranges = [[1, 4], [6, 11]]
recursive_ranges(ranges)
# [1, 2, 3, 6, 7, 8, 9, 10]


Note the *ranges[0] is used to unpack the elements in ranges[0] into individual arguments. Essentially the recursive function keeps grabbing the first element of ranges, each element of which is a two-element array, and passing those numbers into the range() method as two different values instead of one array. That's what the * does, it unpacks the array. First call, you unpack [1, 4] into range(1, 4) and then append the next call of the recursive function to it.



Basically this unpacks into the following:



list(range(1, 4)) + list(range(6, 11))


but you get to use a much more compact syntax, just passing a list of lists.






share|improve this answer





















  • 1





    this will be too long for larger numbers

    – Led
    9 hours ago






  • 1





    @Led what do you mean? Also see my update with the recursive function. Pretty compact.

    – Engineero
    9 hours ago













  • thanks, but there is a need to write a function. Great idea by the way

    – Led
    9 hours ago
















2














You can turn it into a recursive function:



def recursive_ranges(ranges):
if len(ranges) == 1:
return list(range(*ranges[0]))
else:
return list(range(*ranges[0])) + recursive_ranges(ranges[1:])


You can then call this, specifying ranges as a list of lists:



ranges = [[1, 4], [6, 11]]
recursive_ranges(ranges)
# [1, 2, 3, 6, 7, 8, 9, 10]


Note the *ranges[0] is used to unpack the elements in ranges[0] into individual arguments. Essentially the recursive function keeps grabbing the first element of ranges, each element of which is a two-element array, and passing those numbers into the range() method as two different values instead of one array. That's what the * does, it unpacks the array. First call, you unpack [1, 4] into range(1, 4) and then append the next call of the recursive function to it.



Basically this unpacks into the following:



list(range(1, 4)) + list(range(6, 11))


but you get to use a much more compact syntax, just passing a list of lists.






share|improve this answer





















  • 1





    this will be too long for larger numbers

    – Led
    9 hours ago






  • 1





    @Led what do you mean? Also see my update with the recursive function. Pretty compact.

    – Engineero
    9 hours ago













  • thanks, but there is a need to write a function. Great idea by the way

    – Led
    9 hours ago














2












2








2







You can turn it into a recursive function:



def recursive_ranges(ranges):
if len(ranges) == 1:
return list(range(*ranges[0]))
else:
return list(range(*ranges[0])) + recursive_ranges(ranges[1:])


You can then call this, specifying ranges as a list of lists:



ranges = [[1, 4], [6, 11]]
recursive_ranges(ranges)
# [1, 2, 3, 6, 7, 8, 9, 10]


Note the *ranges[0] is used to unpack the elements in ranges[0] into individual arguments. Essentially the recursive function keeps grabbing the first element of ranges, each element of which is a two-element array, and passing those numbers into the range() method as two different values instead of one array. That's what the * does, it unpacks the array. First call, you unpack [1, 4] into range(1, 4) and then append the next call of the recursive function to it.



Basically this unpacks into the following:



list(range(1, 4)) + list(range(6, 11))


but you get to use a much more compact syntax, just passing a list of lists.






share|improve this answer















You can turn it into a recursive function:



def recursive_ranges(ranges):
if len(ranges) == 1:
return list(range(*ranges[0]))
else:
return list(range(*ranges[0])) + recursive_ranges(ranges[1:])


You can then call this, specifying ranges as a list of lists:



ranges = [[1, 4], [6, 11]]
recursive_ranges(ranges)
# [1, 2, 3, 6, 7, 8, 9, 10]


Note the *ranges[0] is used to unpack the elements in ranges[0] into individual arguments. Essentially the recursive function keeps grabbing the first element of ranges, each element of which is a two-element array, and passing those numbers into the range() method as two different values instead of one array. That's what the * does, it unpacks the array. First call, you unpack [1, 4] into range(1, 4) and then append the next call of the recursive function to it.



Basically this unpacks into the following:



list(range(1, 4)) + list(range(6, 11))


but you get to use a much more compact syntax, just passing a list of lists.







share|improve this answer














share|improve this answer



share|improve this answer








edited 9 hours ago

























answered 9 hours ago









EngineeroEngineero

6,48632351




6,48632351








  • 1





    this will be too long for larger numbers

    – Led
    9 hours ago






  • 1





    @Led what do you mean? Also see my update with the recursive function. Pretty compact.

    – Engineero
    9 hours ago













  • thanks, but there is a need to write a function. Great idea by the way

    – Led
    9 hours ago














  • 1





    this will be too long for larger numbers

    – Led
    9 hours ago






  • 1





    @Led what do you mean? Also see my update with the recursive function. Pretty compact.

    – Engineero
    9 hours ago













  • thanks, but there is a need to write a function. Great idea by the way

    – Led
    9 hours ago








1




1





this will be too long for larger numbers

– Led
9 hours ago





this will be too long for larger numbers

– Led
9 hours ago




1




1





@Led what do you mean? Also see my update with the recursive function. Pretty compact.

– Engineero
9 hours ago







@Led what do you mean? Also see my update with the recursive function. Pretty compact.

– Engineero
9 hours ago















thanks, but there is a need to write a function. Great idea by the way

– Led
9 hours ago





thanks, but there is a need to write a function. Great idea by the way

– Led
9 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55654889%2fdefine-a-list-range-inside-a-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...