Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set a list value in a range of integer in python

I want to perform the following:

>>> [0-2, 4]  #case 1
[-2, 4]     #I want the output to be [0, 1, 2, 4]

I know I can perform the same in this way:

>>> list(range(3)) + [4]   #case 2
[0, 1, 2, 4]

But I am curious is there any way to achieve the same result using the case 1 method (or something similar)? Do I need to override the integer '-' operator or do I need to do anything with the list?

like image 831
Allen W Avatar asked Jan 04 '23 03:01

Allen W


1 Answers

>>> [*range(0,3), 4]
[0, 1, 2, 4]

Should come closest. Python 3 only.

like image 140
timgeb Avatar answered May 15 '23 02:05

timgeb