-1

I have this code:

    pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
    (69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
    (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
    (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
    (330, 339), (358, 359), (360, 369)]

Those are given ranges, for example, pg[0] should be 10, pg[1] be 11, pg[2] be 12. and so on for the rest of the ranges. So I want the final array to be like this:

    pg=[10, 11, 12, 13 ....19, 30, 31,....,32,37, 38,50,51,....,59,63.. and so on]

How can I do this in python? Is it possible to do it without hard coding every range of elements in a new array?

Johnny
  • 8,939
  • 2
  • 28
  • 33
John Sall
  • 1,027
  • 1
  • 12
  • 25

6 Answers6

2

Try this

l = []
for r in pg:
    l.extend(range(r[0], r[1]+1))
Benoît P
  • 3,179
  • 13
  • 31
2

This is one approach using a list comprehension and itertools.chain(to flatten the list)

Ex:

from itertools import chain
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
    (69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
    (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
    (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
    (330, 339), (358, 359), (360, 369)]

result = list(chain.from_iterable([range(*i) for i in pg]))
print(result)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
2

A one-linear

 sum([list(range(x1, x2+1)) for x1, x2 in pg], [])
blue_note
  • 27,712
  • 9
  • 72
  • 90
2

I guess the following might work

pg = [(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
    (69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
    (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
    (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
    (330, 339), (358, 359), (360, 369)]
arr = []
for val in pg:
    arr += list(range(val[0], val[1] + 1))
print(arr)
Jeril
  • 7,858
  • 3
  • 52
  • 69
  • why when I only wrtie ar = list(range(val[0],val[1])), it won't work. I mean it gives me an error – John Sall Jan 31 '19 at 11:40
  • it should be `arr += list(range(val[0], val[1] + 1))`, did you notice `+=` – Jeril Jan 31 '19 at 11:41
  • yes I did, I'm asking about the above code, I'm just asking if I write the following without the for loop: arr = list(range(pg[0], pg[1] + 1)) because I want to understand how this is working. if I write this it gives an error. notice I put pg instead of val – John Sall Jan 31 '19 at 11:44
  • Kindly go through the [post](https://stackoverflow.com/q/4841436/2825570), for better understanding. – Jeril Jan 31 '19 at 12:26
1

One more example using list comprehension

a = [(10, 19), (30, 35)]
b = [j for i in a for j in range(i[0], i[1]+1)]
print(b) 

#output
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 35]
Johnny
  • 8,939
  • 2
  • 28
  • 33
0

with help of sum function also we can achieve this.

pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
    (69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
    (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
    (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
    (330, 339), (358, 359), (360, 369)]      

print (list(sum(pg,())))

#output:[10, 19, 30, 32, 37, 38, 50, 59, 63, 64, 69, 69, 120, 121, 124, 129, 130, 139, 
#160, 161, 164, 169, 180, 182, 185, 185, 189, 189, 190, 192, 194, 194, 196, 199, 260, 
#269, 270, 279, 300, 309, 330, 339, 358, 359, 360, 369]
vinodsesetti
  • 368
  • 2
  • 6