Set literal confusion

Vlastimil Brom vlastimil.brom at gmail.com
Tue Sep 22 19:35:59 CEST 2009


2009/9/22 kaoruAngel <kaoruangel at gmail.com>:
> I recently decided to implement a small project in python after being
> away from the language for a while, so, in learning the language over
> again, I experimented.
>
> ---------------------------------------
> Python 3.1.1 (r311:74483, Aug 17 2009, 16:45:59) [MSC v.1500 64 bit
> (AMD64)] on win32
>
>>>> 5 in set(range(10))
> True
>
>>>> 15 in set(range(10))
> False
>
>>>> 5 in {range(10)}
> False
>
>>>> 15 in {range(10)}
> False
>
>>>> {range(10)}
> {range(0, 10)}
>
>>>> set(range(10))
> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
> ------------------------------------------
>...
>
You might also try the set comprehension:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> {i for i in range(10)}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> set(range(10))
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>>>

For these simple cases without conditions, it probably doesn't make
much difference.
The character of the single items is maybe more obvious.

vbr



More information about the Python-list mailing list