Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> ].
Source Link
Peter Mortensen

If you feel negative indices in slicing is confusing, here's a very easy way to think about it: just replace the negative index with len - index. So for example, replace -3 with len(list) - 3.

The best way to illustrate what slicing does internally is just show it in code that implements this operation:

def slice(list, start = None, end = None, step = 1):
  # takeTake care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # takeTake care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # nowNow just execute a for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  

If you feel negative indices in slicing is confusing, here's very easy way to think about it: just replace negative index with len - index. So for example, replace -3 with len(list) - 3.

The best way to illustrate what slicing does internally is just show it in code that implements this operation:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  

If you feel negative indices in slicing is confusing, here's a very easy way to think about it: just replace the negative index with len - index. So for example, replace -3 with len(list) - 3.

The best way to illustrate what slicing does internally is just show it in code that implements this operation:

def slice(list, start = None, end = None, step = 1):
  # Take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # Take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # Now just execute a for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
added 2 characters in body
Source Link
Shital Shah

If you feel negative indices in slicing is bit "unnatural"confusing, here's very easy way to think about it: just replace negative index with len - indexlen - index. So for example, replace -3 with len(list) - 3. I think

The best way to illustrate what slicing does internally is just write someshow it in code that will give exactly the same behaviourimplements this operation:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  

If you feel negative indices in slicing is bit "unnatural", here's very easy way to think about it: just replace negative index with len - index. So for example, replace -3 with len(list) - 3. I think best way to illustrate what slicing does internally is just write some code that will give exactly the same behaviour:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  

If you feel negative indices in slicing is confusing, here's very easy way to think about it: just replace negative index with len - index. So for example, replace -3 with len(list) - 3.

The best way to illustrate what slicing does internally is just show it in code that implements this operation:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  
deleted 9 characters in body
Source Link
Shital Shah

Many people tend toIf you feel uncomfortable with negative indices in slicing. The solution for that is bit "unnatural", here's very easy way to think about it: just replace negative index with len - index. So for example, replace -3 with len(list) - 3. I think best way to illustrate what slicing does internally is just write some code that will give exactly the same behaviour:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  

Many people tend to feel uncomfortable with negative indices in slicing. The solution for that is very easy: just replace negative index with len - index. So for example, replace -3 with len(list) - 3. I think best way to illustrate what slicing does internally is just write some code that will give exactly the same behaviour:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  

If you feel negative indices in slicing is bit "unnatural", here's very easy way to think about it: just replace negative index with len - index. So for example, replace -3 with len(list) - 3. I think best way to illustrate what slicing does internally is just write some code that will give exactly the same behaviour:

def slice(list, start = None, end = None, step = 1):
  # take care of missing start/end parameters
  start = 0 if start is None else start
  end = len(list) if end is None else end

  # take care of negative start/end parameters
  start = len(list) + start if start < 0 else start
  end = len(list) + end if end < 0 else end

  # now just execute for-loop with start, end and step
  return [list[i] for i in range(start, end, step)]
  
added 15 characters in body
Source Link
Shital Shah
Loading
deleted 10 characters in body
Source Link
Shital Shah
Loading
Source Link
Shital Shah
Loading
lang-py