473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

slice last 4 items from a list

Is this the correct way to slice the last 4 items from a list?

x = [1,2,3,4,5,6,7,8,9]
print x[-4:]

It works, but is it Pythonic?
Oct 1 '07 #1
3 1690
brad a écrit :
Is this the correct way to slice the last 4 items from a list?

x = [1,2,3,4,5,6,7,8,9]
print x[-4:]

It works, but is it Pythonic?
Is there a more obvious (for a pythonic definition of 'obvious') way to
do it ? If no, then it's pythonic... Now FWIW, I usually use
'esrever'[::-1] to reverse a string, so I may not be a reference !-)
Oct 1 '07 #2
On Oct 2, 6:27 am, brad <byte8b...@gmail.comwrote:
Is this the correct way to slice the last 4 items from a list?

x = [1,2,3,4,5,6,7,8,9]
print x[-4:]

It works, but is it Pythonic?
It's Pythonic. It's also unambiguous, unlike your specification, which
could be interpreted as 'chop off the last 4 items from a list' i.e.
del x[-4:]. What you are actually doing is 'print the last 4 items of
a list'.

Oct 2 '07 #3
In message <11**********************@g4g2000hsf.googlegroups. com>, John
Machin wrote:
On Oct 2, 6:27 am, brad <byte8b...@gmail.comwrote:
>Is this the correct way to slice the last 4 items from a list?

x = [1,2,3,4,5,6,7,8,9]
print x[-4:]

It works, but is it Pythonic?

It's Pythonic. It's also unambiguous, unlike your specification, which
could be interpreted as 'chop off the last 4 items from a list' ...
No, the OP is using "slice" in the technically correct CS sense.
Oct 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
0
by: Rich Krauter | last post by:
I do not understand why python behaves the way it does in the following code: Here, Python doesn't like that I'm assigning out-of-range. OK. That's fine. >>> x = >>> x = 3 Traceback (most...
1
by: Steven Bethard | last post by:
Is there a reason that itertools.islice doesn't support None arguments for start and step? This would be handy for use with slice objects: >>> r = range(20) >>> s1 = slice(2, 10, 2) >>> s2 =...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
23
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in...
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
3
by: Bas | last post by:
Hi, stupid question, but would it be possible to somehow merge xrange (which is supposed to replace range in py3k) and slice? Both have very similar start, stop and step arguments and both are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.