From 7e86e24f1750bc0df3584812b012d4d802079ba3 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 13 Dec 2008 09:26:01 +0100 Subject: [PATCH] set and dict comprehensions are supported --- docs/pyrex_differences.rst | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/pyrex_differences.rst b/docs/pyrex_differences.rst index 415e8164..e863d91b 100644 --- a/docs/pyrex_differences.rst +++ b/docs/pyrex_differences.rst @@ -6,14 +6,25 @@ Differences between Cython and Pyrex ************************************** -List Comprehensions -==================== +List/Set/Dict Comprehensions +============================= + +Cython supports the different comprehensions defined by Python 3.0 for +lists, sets and dicts:: + + [expr(x) for x in A] # list + {expr(x) for x in A} # set + {key(x) : value(x) for x in A} # dict + +Looping is optimized if ``A`` is a list, tuple or dict. You can use +the :keyword:`for` ... :keyword:`from` syntax, too, but it is +generally preferred to use the usual :keyword:`for` ... :keyword:`in` +``range(...)`` syntax with a C run variable (e.g. ``cdef int i``). + +.. note:: see :ref:`automatic-range-conversion` -`[expr(x) for x in A]` is now available, implementing the full specification -at http://www.python.org/dev/peps/pep-0202/ . Looping is optimized if ``A`` is -a list. Also, use the :keyword:`for` ... :keyword:`from` syntax too, e.g.:: +Note that Cython also supports set literals starting from Python 2.3. - [i*i for i from 0 <= i < 10] Conditional expressions "x if b else y" (python 2.5) ===================================================== -- 2.26.2