Python idiom for taking the single item from a list

  • Personally, I think this is a bit on the "clever" side. Plus, the error message you get isn't as easy to understand as if you used an assert statement. I'd probably just do something like this:

        def get_single(l):
            assert l and len(l) == 1
            return l[0]
    
    Then you get the best of both worlds: readability and a concise one-liner.

  • Excellent. That one belongs in any Python style guide. Though technically it's not a style, it does lead to better readability, and reduces the propensity for unforseen consequences.

  • The real solution is not to arbitrarily encode your types as lists of exactly one item. If you find yourself passing around such lists with enough regularity that you feel the need to develop an idiom for deconstructing them, you're doing something wrong.

  • I think this style has one great drawback: it requires deeper knowledge of Python then the 'usual' thing = stuff[0].

  • Very good idea, I support this and will use it from now on (even though I don't like the (thing,) Python syntax very much), as it's the only way to say that the iterable should only have one element.

  • I may not be Dutch enough, but this certainly isn't the obvious way to do it.

  • [thing] = stuff

    also works and is more readable IMHO.

  • So we have

    so if I was wrong in my original assumption that stuff has exactly one element, Python will shout at me before this will manifest itself as a hard-to-find bug someplace else in the program.

    and then later on,

    This method works even when stuff is a set or any other kind of collection. stuff[0] wouldn’t work on a set because set doesn’t support access by index number.

    The second argument is basically in favor of duck-typing which is the pythonic way of writing code, ie, not caring about the actual object but only if it responds to the given message.

    But what about the first argument? Couldn't you make the case that the pythonic way of handling it is to only care about if the object responds to __getitem__(0)?

    Which idiom you use would depend entirely on context, wouldn't it?

  • My problem is that exact situation almost never comes up. Usually, I want to use the item in the list directly in an expression, not assign it to another variable first. For example:

        if 1==len(lst):
            return lst[0]
        else:
            return reduce(fn, lst)
    
    I don't really want to have to do:

        if 1==len(lst):
            (single,)=lst
            return single
        else:
            return reduce(fn, lst)

  • Single-element tuple unpacking?

    I've never found a case where I knew there would only be one element in a list, but I do use tuple unpacking all of the time.

    Pretty cool I guess; didn't know it wasn't a common idiom. Good to know. Thanks for sharing. :)

  • Seems he would have more fun being a Rubyist.

  • I prefer array.single(), I think it's a rare enough use-case that spelling out what you are doing is worthwhile.

  • The same code works in ruby, though without the assertion behavior:

        > a = [0]
        > b, = a
        > b == 0
         => true

  • There you have it folks, Python supports destructuring assignment. :-)

  • undefined

  • Proposed style guide:

    When you want to get the (n+1)th item from a list, do:

    item = stuff[n]

    To get the first item, do:

    item = stuff[0]

    Unless the list has one element, then do:

    (item, ) = stuff

    At least you'll never be accused of consistency.

  • thing, = stuff works too.