Skip to content Skip to sidebar Skip to footer

Python Combining A Range And A List Of Numbers

range(5, 15) [1, 1, 5, 6, 10, 10, 10, 11, 17, 28] range(6, 24) [4, 10, 10, 10, 15, 16, 18, 20, 24, 30] range(7, 41) [9, 18, 19, 23, 23, 26, 28, 40, 42, 44] range(11, 49) [9, 23, 24

Solution 1:

The range() function returns a special range object to save on memory (no need to keep all the numbers in memory when only the start, end and step size will do). Cast it to a list to 'expand' it:

list(yourrange) + otherlist

To quote the documentation:

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

Post a Comment for "Python Combining A Range And A List Of Numbers"