Set Comprehensions

Similarly to list comprehensions, set comprehensions are also supported:

>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
set(['r', 'd'])

Syntax

{expr for iter_var in iterable}
{expr for iter_var in iterable if cond_expr}

References

[1] PEPS@Python Developer's Guide, PEP 218 -- Adding a Built-In Set Object Type

[2] Docs@Python, 5. Data Structures — 5.4. Sets

results matching ""

    No results matching ""