Python list initialization using multiple range statements

后端 未结 7 1537
误落风尘
误落风尘 2021-01-07 22:15

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)]

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-07 22:57

    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]
    

提交回复
热议问题