[portland] Extracting strings from a tuple

Matt McCredie mccredie at gmail.com
Sat Nov 3 00:18:16 CET 2007


> [a[0][i] for i in range(len(a[0]))]
>

wouldn't that be better written:

list(a[0])

In python if you find yourself doing 'range(len(something))' it is
time to backup and re-evaluate. Heck, you can even use list
comprehension without the range(len(a[0])):

[val for val in a[0]]

> To unpack that, it's a list comprehension which look like this:
>
> [a for a in listofthings ]

What are you doing here? Just making a copy? That is better written:

x = listofthings[:]

I'm not trying to be a pain, just trying to encourage more pythonic habits.

Matt


More information about the Portland mailing list