0

NEED HELP IN SLICING AND REVERSE

number = list (range(10)) print (number[1:7:-1]) Output is [] Expecting: [6,5,4,3,2,1] But number = list (range(10)) number2 = number[1:7] print (number2[::-1]) Output is [6,5,4,3,2,1] I have to make a separate variable just to reverse the list ? Is there any other way ? What’s an alternate way to do reverse step slicing ? In simple words new list with starting 6 digits in reverse order from the original list.

24th Jun 2020, 11:34 AM
Aqib Mohammed RJ
2 Antworten
+ 3
Instead print(number[-4:-10:-1])
24th Jun 2020, 12:04 PM
Abhay
Abhay - avatar
+ 2
You don't have to create a new variable, instead switch the places in the slicing: nums = list(range(10)) print(nums[7:0:-1]) This leads to the wished for output.
24th Jun 2020, 2:15 PM
loeckchen[INACTIVE]