[Tutor] Should this be a list comprehension or something?

Jacob S. keridee at jayco.net
Sat Jan 29 04:37:15 CET 2005


>
>> Its not so much a criterion that they *should* be used that way,
>> its just that its what they do. A list comprehension creates a list!
>> Thats why they are called *list* comprehensions. :-)
>
> See, I'd always figured that the reason it was called a list
> comprehension was because the list comprehension operated on a list,
> and the operation was comprehension.
>

It can easily be checked by...

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = range(10)
>>> b = [x for x in a if x % 2 == 0]
>>> c = ['12','21','14','13']
>>> d = [a[2] for x in c]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b
[0, 2, 4, 6, 8]
>>> c
['12', '21', '14', '13']
>>> d
[2, 2, 2, 2]
>>> type(a)
<type 'list'>
>>> type(b)
<type 'list'>
>>> type(c)
<type 'list'>
>>> type(d)
<type 'list'>
>>>

Everything I did with list comprehensions above shows that list 
comprehensions return a list.
Mostly it is evidenced by the type function...

Maybe, this post is silly?
Oh, well.
Jacob 



More information about the Tutor mailing list