What's correct Python syntax?

Ned Batchelder ned at nedbatchelder.com
Tue Jan 14 07:19:42 EST 2014


On 1/14/14 6:33 AM, Peter Otten wrote:
> Python has no dedicated syntax for picking arbitrary items from a list
> If you are only concerned about printing use format():
>
>>>> >>>items = ["alpha", "beta", "gamma", "delta"]
>>>> >>>print "{1} {3} {0}".format(*items)
> beta delta alpha

.format also supports item access directly:

 >>> items = ["alpha", "beta", "gamma", "delta"]
 >>> print "{0[1]} {0[3]} {0[0]}".format(items)
beta delta alpha

It's clunkier in this example, but if you have more than one value being 
formatted, this (and the "{0.foo}" syntax) can make digging into nested 
data more convenient.

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list