@@ -333,6 +333,47 @@ The :func:`zip` function would do a great job for this use case::
333333
334334See :ref: `tut-unpacking-arguments ` for details on the asterisk in this line.
335335
336+ Unpacking in Lists and List Comprehensions
337+ ------------------------------------------
338+
339+ The section on :ref: `tut-unpacking-arguments ` describes the use of ``* `` to
340+ "unpack" the elements of an iterable object, providing each one seperately as
341+ an argument to a function. Unpacking can also be used in other contexts, for
342+ example, when creating lists. When specifying elements of a list, prefixing an
343+ expression by a ``* `` will unpack the result of that expression, adding each of
344+ its elements to the list we're creating::
345+
346+ >>> x = [1, 2, 3]
347+ >>> [0, *x, 4, 5, 6]
348+ [0, 1, 2, 3, 4, 5, 6]
349+
350+ This only works if the expression following the ``* `` evaluates to an iterable
351+ object; trying to unpack a non-iterable object will raise an exception::
352+
353+ >>> x = 1
354+ >>> [0, *x, 2, 3, 4]
355+ Traceback (most recent call last):
356+ File "<python-input-1>", line 1, in <module>
357+ [0, *x, 2, 3, 4]
358+ TypeError: Value after * must be an iterable, not int
359+
360+ Unpacking can also be used in list comprehensions, as a way to build a new list
361+ representing the concatenation of an arbitrary number of iterables::
362+
363+ >>> x = [[1, 2, 3], [4, 5, 6], [], [7], [8, 9]]
364+ >>> [*element for element in x]
365+ [1, 2, 3, 4, 5, 6, 7, 8, 9]
366+
367+ Note that the effect is that each element from ``x `` is unpacked. This works
368+ for arbitrary iterable objects, not just lists::
369+
370+ >>> x = [[1, 2, 3], 'cat', {'spam': 'eggs'}]
371+ >>> [*element for element in x]
372+ [1, 2, 3, 'c', 'a', 't', 'spam']
373+
374+ But if the objects in ``x `` are not iterable, this expression would again raise
375+ an exception.
376+
336377.. _tut-del :
337378
338379The :keyword: `!del ` statement
@@ -394,7 +435,10 @@ A tuple consists of a number of values separated by commas, for instance::
394435 >>> v = ([1, 2, 3], [3, 2, 1])
395436 >>> v
396437 ([1, 2, 3], [3, 2, 1])
397-
438+ >>> # they support unpacking just like lists:
439+ >>> x = [1,2,3]
440+ >>> 0, *x, 4
441+ (0, 1, 2, 3, 4)
398442
399443As you see, on output tuples are always enclosed in parentheses, so that nested
400444tuples are interpreted correctly; they may be input with or without surrounding
@@ -480,12 +524,16 @@ Here is a brief demonstration::
480524 {'r', 'd', 'b', 'm', 'z', 'l'}
481525
482526Similarly to :ref: `list comprehensions <tut-listcomps >`, set comprehensions
483- are also supported::
527+ are also supported, including comprehensions with unpacking ::
484528
485529 >>> a = {x for x in 'abracadabra' if x not in 'abc'}
486530 >>> a
487531 {'r', 'd'}
488532
533+ >>> fruits = [{'apple', 'avocado', 'apricot'}, {'banana', 'blueberry'}]
534+ >>> {*fruit for fruit in fruits}
535+ {'blueberry', 'banana', 'avocado', 'apple', 'apricot'}
536+
489537
490538.. _tut-dictionaries :
491539
@@ -563,6 +611,18 @@ arbitrary key and value expressions::
563611 >>> {x: x**2 for x in (2, 4, 6)}
564612 {2: 4, 4: 16, 6: 36}
565613
614+ And dictionary unpacking (via ``** ``) can be used to merge multiple
615+ dictionaries::
616+
617+ >>> odds = {i: i**2 for i in (1, 3, 5)}
618+ >>> evens = {i: i**2 for i in (2, 4, 6)}
619+ >>> {**odds, **evens}
620+ {1: 1, 3: 9, 5: 25, 2: 4, 4: 16, 6: 36}
621+
622+ >>> all_values = [odds, evens, {0: 0}]
623+ >>> {**i for i in all_values}
624+ {1: 1, 3: 9, 5: 25, 2: 4, 4: 16, 6: 36, 0: 0}
625+
566626When the keys are simple strings, it is sometimes easier to specify pairs using
567627keyword arguments::
568628
0 commit comments