Python Forum
Preferred way to slice a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preferred way to slice a list
#1
In the Zen of Python, it states:

Quote:There should be one-- and preferably only one --obvious way to do it.

Suppose I have the following list:

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
I can slice it to print the last 4 digits in two ways:

# Both will print ['e', 'f', 'g', 'h']
print(a[-4:])
print(a[4:])
Which is the Pythonic way of doing it?

I don't like the a[4:] because at first glance it looks like I'm printing the first 4 elements of the list.
Reply
#2
Don't worry about the one true way to do things. That is probably the most often broken part of the Zen of Python.

I don't think either [4:] or [-4:] is more Pythonic. Here's how I would think of it. The colon is to the right, so it's printing everything to the right of that index. The first one prints everything after the fourth item, and the second one prints the last four items (because of the negative). If your intent is to get 'the last four digits', then I think [-4:] is clearer. More important than that is what if you get a 9 item list? or a 7 item list. In either of those cases, [-4:] will give you the correct number of items, but [4:] may not give you the right number of items.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I would add that for better readability one can use slice():

>>> lst = list(“abcde”)
>>> last_four = slice(-4, None)
>>> lst[last_four]
['b', 'c', 'd', 'e']
However, one should keep in mind that no error is raised when list is shorter than four:

>>> lst = list(“abc”)
>>> lst[-4:]
['a', 'b', 'c']
>>> lst[last_four]
['a', 'b', 'c']
>>> lst = list()
>>> lst[-4:]
[]
Code can’t rely on assumption that length of returned list is four.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Dec-09-2019, 10:12 PM)ichabod801 Wrote: In either of those cases, [-4:] will give you the correct number of items, but [4:] may not give you the right number of items.

I ran into that exact problem when I was experimenting on two different lists. I'm embarrassed to say it took me a moment to realize what was going on.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 816 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Slice creates new objects? fmr300 4 1,296 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,392 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Slice list Moris526 1 1,641 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,089 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Pass Tuple as a Slice nagymusic 2 2,356 Dec-12-2019, 04:42 AM
Last Post: nagymusic
  slice python array on condition Gigux 2 2,257 Nov-03-2019, 07:21 PM
Last Post: Larz60+
  How to append and drop to next line while slice/indexing emryscass 3 2,589 Sep-26-2019, 01:06 PM
Last Post: Malt
  Is Event.set() the preferred way to stop a thread? svetlanarosemond 5 3,823 Jul-17-2018, 08:14 AM
Last Post: DeaD_EyE
  Understanding slice copying in for loop yksingh1097 5 4,025 Jul-02-2018, 01:03 PM
Last Post: yksingh1097

Forum Jump:

User Panel Messages

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