Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sequence slicing problem
#2
As I understand it, the negative step effectively reserves the sequence. Then, it iterates over the reserved sequence until it reaches the fifth index. You may expect that to be [10, 8, 6], but counting backwards through the sequence starts at index -1 so the 6 gets cut off.

To achieve the desired output, you'll need to do this:

numbers[:5][::-2]
Reply


Messages In This Thread
sequence slicing problem - by qliu - Apr-27-2020, 08:14 PM
RE: sequence slicing problem - by stullis - Apr-27-2020, 09:08 PM
RE: sequence slicing problem - by deanhystad - Apr-27-2020, 09:23 PM
RE: sequence slicing problem - by Yoriz - Apr-27-2020, 09:30 PM
RE: sequence slicing problem - by deanhystad - Apr-27-2020, 09:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String slicing problem Ollew 4 2,782 Sep-08-2018, 08:07 PM
Last Post: Ollew
  Problem with slicing string YatishK 2 3,390 Apr-24-2017, 09:46 AM
Last Post: YatishK

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020