Search code examples
pythoncoding-styleiterable-unpacking

A Function Returns a List with one Value. How to assign it?


numpy.correlate returns an array of length 1 for arguments of equal length. What is the best way to assign that one value, but not the array?

  • [result] = numpy.correlate([1], [1])
  • result, = numpy.correlate([1], [1])
  • result = numpy.correlate([1], [1])[0]

If I understand PEP 448 in Python 3.5 correctly, this would additionally allow:

  • result = *numpy.correlate([1], [1])

Is there a canonical correct way of writing this?


Solution

  • The form:

    result = numpy.correlate([1], [1])[0]
    

    is more similar to other languages, so, in my opinion, it is easier to read and understand.