Python question

15 views
Skip to first unread message

Mukta Sane

unread,
Sep 20, 2025, 12:07:12 PMSep 20
to leetcode-meetup
Hello friends,

I am learning Python and have one doubt if someone could help. If this is not appropriate question for this GG, could someone suggest where I may ask such questions (Google/AI did not help)

Using list slicing feature, is it possible to reverse a sub-portion of the list? 

e.g. array = [1, 2, 3, 4, 5]
print(array[::-1]) --> 5, 4, 3, 2, 1

likewise, I used print(array[-4:-1]) to get sub-array 2, 3, 4

if I wanted 4, 3, 2 instead, is there a way to achieve it using slicing, if not, other than the traditional for loop, does Python have another way of doing it? I have learned list comprehension but not sure if it can help in such scenarios.

Thanks.


Xiaxi Shen

unread,
Sep 20, 2025, 12:28:22 PMSep 20
to leetcod...@googlegroups.com
Hi Mukta,

Following are two ways I know so far

list(reversed(a[-4:-1]))

a[-4:-1][::-1]





--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/leetcode-meetup/49149851-1732-487f-a219-dbc93209cf20n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages