FAQ
I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

--
Grant Edwards grante Yow! I want a VEGETARIAN
at BURRITO to go ... with
visi.com EXTRA MSG!!

Search Discussions

  • Akonsu at Sep 23, 2009 at 6:58 pm

    On Sep 23, 2:47?pm, Grant Edwards wrote:
    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:

    ? ?s.rsplit(None,1)[1]

    It was somewhat obvious from the context what it was supposed
    to do, but it took a bit of Googling to figure out exactly what
    was going on.

    When I want the last word in a string, I've always done this:

    ? ?s.split()[-1]

    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be other than inducing people to learn about the
    maxsplit argument that is accepted by the split() methods?

    --
    Grant Edwards ? ? ? ? ? ? ? ? ? grante ? ? ? ? ? ? Yow! I want a VEGETARIAN
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? at ? ? ? ? ? ? ? BURRITO to go ... with
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?visi.com ? ? ? ? ? ?EXTRA MSG!!
    hello,
    perhaps rsplit generates as many elements in the list as absolutely
    necesary compared to the whole list returned by split()?
    konstantin
  • Peter Otten at Sep 23, 2009 at 7:22 pm

    akonsu wrote:
    On Sep 23, 2:47 pm, Grant Edwards wrote:
    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:

    s.rsplit(None,1)[1]

    It was somewhat obvious from the context what it was supposed
    to do, but it took a bit of Googling to figure out exactly what
    was going on.

    When I want the last word in a string, I've always done this:

    s.split()[-1]

    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be other than inducing people to learn about the
    maxsplit argument that is accepted by the split() methods?

    --
    Grant Edwards grante Yow! I want a
    VEGETARIAN at BURRITO to go ... with
    visi.com EXTRA MSG!!
    hello,
    perhaps rsplit generates as many elements in the list as absolutely
    necesary compared to the whole list returned by split()?
    konstantin
    Indeed, and if the string is long it has a measurable effect:

    $ python -m timeit -s"s = 'oneword '*1000" "s.rsplit(None, 1)[-1]"
    100000 loops, best of 3: 2.23 usec per loop
    $ python -m timeit -s"s = 'oneword '*1000" "s.split()[-1]"
    1000 loops, best of 3: 191 usec per loop

    Peter
  • Christos Trochalakis at Sep 23, 2009 at 7:16 pm

    At Wed, 23 Sep 2009 18:47:05 +0000 (UTC), Grant Edwards wrote:

    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:

    s.rsplit(None,1)[1]

    It was somewhat obvious from the context what it was supposed
    to do, but it took a bit of Googling to figure out exactly what
    was going on.

    When I want the last word in a string, I've always done this:

    s.split()[-1]

    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be other than inducing people to learn about the
    maxsplit argument that is accepted by the split() methods?
    s.rsplit(None, 1) is a cheaper operation because it splits the string
    *only once*.
  • Christos Trochalakis at Sep 23, 2009 at 7:16 pm

    At Wed, 23 Sep 2009 18:47:05 +0000 (UTC), Grant Edwards wrote:

    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:

    s.rsplit(None,1)[1]

    It was somewhat obvious from the context what it was supposed
    to do, but it took a bit of Googling to figure out exactly what
    was going on.

    When I want the last word in a string, I've always done this:

    s.split()[-1]

    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be other than inducing people to learn about the
    maxsplit argument that is accepted by the split() methods?
    s.rsplit(None, 1) is a cheaper operation because it splits the string
    *only once*.
  • Christos Trochalakis at Sep 23, 2009 at 7:17 pm

    At Wed, 23 Sep 2009 18:47:05 +0000 (UTC), Grant Edwards wrote:

    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:

    s.rsplit(None,1)[1]

    It was somewhat obvious from the context what it was supposed
    to do, but it took a bit of Googling to figure out exactly what
    was going on.

    When I want the last word in a string, I've always done this:

    s.split()[-1]

    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be other than inducing people to learn about the
    maxsplit argument that is accepted by the split() methods?
    s.rsplit(None, 1) is a cheaper operation because it splits the string
    *only once*.
  • Christos Trochalakis at Sep 23, 2009 at 7:22 pm

    At Wed, 23 Sep 2009 18:47:05 +0000 (UTC), Grant Edwards wrote:

    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:

    s.rsplit(None,1)[1]

    It was somewhat obvious from the context what it was supposed
    to do, but it took a bit of Googling to figure out exactly what
    was going on.

    When I want the last word in a string, I've always done this:

    s.split()[-1]

    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be other than inducing people to learn about the
    maxsplit argument that is accepted by the split() methods?
    s.rsplit(None, 1) is a cheaper operation because it splits the string
    *only once*.
  • Scott David Daniels at Sep 29, 2009 at 2:19 pm

    Grant Edwards wrote:
    I recently ran across this construct for grabbing the last
    (whitespace delimited) word in a string:
    s.rsplit(None,1)[1]
    ... I've always done this:
    s.split()[-1]
    I was wondering what the advantage of the rsplit(None,1)[1]
    approach would be ...
    Others have pointed out the efficiency reason (asking the machine
    to do a pile of work that you intend to throw away). But nobody
    warned you:
    s.rsplit(None, 1)[-1]
    would be better in the case of 'single_word'.rsplit(None, 1)

    --Scott David Daniels
    Scott.Daniels at Acm.Org

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 23, '09 at 6:47p
activeSep 29, '09 at 2:19p
posts8
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase