↓ Skip to main content
  1. Posts/

Unpacking Inside Python 3.15 Comprehensions

··166 words·1 min·

🐍 Python 3.15 lets you use * to unpack elements inside comprehensions. This makes it easier to flatten a list of lists, for example, without writing two loops: each sublist contributes its elements directly to the result.

The same idea works with set comprehensions and generator expressions. For dictionaries, use ** to combine key-value pairs from several dictionaries into one. If a key appears more than once, the last value wins.

The author notes that this syntax can replace some patterns using extend, update, or itertools.chain.from_iterable. Those alternatives remain available in earlier Python versions.

💡 Explanation in a nutshell
#

Imagine several boxes of objects that you want to gather in one place. Unpacking takes the contents of each box and adds them to the final collection. The comprehension says where the boxes come from, and the asterisk expands their contents.

More information at the link 👇

Also published on LinkedIn.

Juan Pedro Bretti Mandarano
Author
Juan Pedro Bretti Mandarano