Skip to main content

Transforming a Collection

A transformation takes a collection, such as a list or a dictionary, and builds a new collection from it. The original is not changed.

In Python the main tool is the comprehension: a short expression in square brackets that builds a new list, or in curly braces for a new dictionary. Most transformations on this page are one line long. The built-in functions zip and enumerate and the string method join cover the rest.

Python also has a built-in map() function that applies a function to every element. It works, but a comprehension says the same thing more clearly and can also skip elements, so this page uses comprehensions.


Map

The map operation applies the same function to every element and collects the answers in a new list of the same length. For example, it can turn [1, 2, 3] into [3, 6, 9].

In Python you write a list comprehension: [x * 3 for x in numbers]. It reads almost like English: the new list holds x * 3 for every x in numbers.

Do not confuse this with Python's map() built-in, which does the same job by taking a function object. Here "map" means "apply an expression to every element".

Map with a list comprehension

The comprehension evaluates the expression on every element and collects the results in a new list. Here it multiplies each number by 3.

Prompt
Write a Python program with the list 1, 2, 3. Use a list comprehension to multiply each number by 3 and print the new list.
Output: complete Python program
Map with a list comprehension

Map with the index

Sometimes the position of an element matters too. enumerate gives you both the index and the value, so the comprehension can use both. Here each value is multiplied by its own index.

Prompt
Loop over the list 1, 2, 3 with enumerate, using both the index and the value. Build a new list where each element is value * index, then print it.
Output: complete Python program
Map with the index

Skip some results

Sometimes some results should be dropped. In Python a function can return None to say "no result". The first comprehension keeps every answer, None included. The second one has an if part that keeps only the results that are not None.

Prompt
Write a function that returns None for the number 2 and three times the number otherwise. Map it over the list 1, 2, 3 and print the raw results, then print a filtered list without the None.
Output: complete Python program
Skip some results

Transform dictionary keys

To change the keys of a dictionary, build a new dictionary with a dict comprehension over items(). Here upper() turns each key into uppercase. A dictionary keeps its entries in insertion order, so the output is always in the same order.

Prompt
Start with a dictionary from "key1" to 1 and "key2" to 2. Build a new dictionary with the same values but every key changed to uppercase with upper(). Print the new dictionary.
Output: complete Python program
Transform dictionary keys

Transform dictionary values

To change the values, do the same but keep the key and compute a new value. Here each value grows by the length of its key: 1 + len("key1") is 5.

Prompt
Start with a dictionary from "key1" to 1 and "key2" to 2. Build a new dictionary with the same keys where each value is the old value plus the length of its key. Print the new dictionary.
Output: complete Python program
Transform dictionary values

Zip

Zipping takes two lists and pairs up their elements by position: the first with the first, the second with the second, and so on. The result has as many pairs as the shorter list.

Python has a built-in zip function. It returns a lazy iterator of tuples, so wrap it in list() when you want to see or reuse the pairs.

Zip two lists

zip pairs one color with one animal and stops at the shorter list by itself. Each pair is a tuple, printed in round brackets.

Prompt
Combine the lists red, brown, grey and fox, bear, wolf element by element with the zip built-in and print the resulting list of pairs.
Output: complete Python program
Zip two lists

Zip with a transformation

You do not always need the pairs themselves. Here a comprehension loops over zip(colors, animals) and builds a sentence from each pair. capitalize() makes the first letter of the animal name uppercase.

Prompt
Combine the lists red, brown, grey and fox, bear, wolf element by element. For each pair print a sentence like "The Fox is red", with the first letter of the animal in uppercase.
Output: complete Python program
Zip with a transformation

Unzip a list of pairs

Unzipping is the reverse: one list of pairs becomes two collections. The star in zip(*number_pairs) unpacks the list, so zip receives the three pairs as separate arguments and regroups their first parts and their second parts. The two results are tuples.

Prompt
Start with a list of pairs: one with 1, two with 2, three with 3. Split it into the words and the numbers with zip(*pairs) and print each result on its own line.
Output: complete Python program
Unzip a list of pairs

Associate

Associating builds a dictionary from a list. Each element gives you one key and one value. A dictionary cannot hold the same key twice, so if two elements produce the same key, the later one wins.

In Python you write a dict comprehension: {key: value for element in items}, where the key and the value are computed from each element.

Elements as keys

The element itself becomes the key, and a value is computed from it. Here the words are keys and their lengths are values.

Prompt
Build a dictionary from the words one, two, three where each word is a key and its length is the value. Print the dictionary.
Output: complete Python program
Elements as keys

Elements as values

The element becomes the value, and the key is computed from it. Here the key is the uppercase first letter. Both two and three start with T, so three replaces two and the dictionary has only two entries.

Prompt
Build a dictionary from the words one, two, three where the key is the uppercase first letter of the word and the value is the word itself. Print the dictionary and notice that "three" replaces "two" under the key T.
Output: complete Python program
Elements as values

Compute both key and value

You can compute both the key and the value from the element. Here the key is the first letter and the value is the word length. Again the later T entry wins.

Prompt
Build a dictionary from the words one, two, three where the key is the uppercase first letter and the value is the length of the word. Print the dictionary.
Output: complete Python program
Compute both key and value

Key and value from one element

Sometimes one element carries both parts. split() cuts a full name on spaces into a list of words, and unpacking names the two parts. The last name becomes the key and the first name becomes the value.

Prompt
Start with the full names "Alice Adams" and "Brian Brown". Split each name into words with split() and build a dictionary from last name to first name. Print the dictionary.
Output: complete Python program
Key and value from one element

Flatten

Flattening turns a nested collection, such as a list of lists, into one plain list with all the elements in order.

A comprehension can have two for parts: [n for inner in number_sets for n in inner]. Read them left to right: for every inner list, for every element in it, keep the element.

Flatten a list of lists

The comprehension visits each inner list and collects all of its elements into result. Two inner lists of three numbers become one list of six.

Prompt
Start with a list of lists: 1, 2, 3 and 4, 5, 6. Flatten it into one plain list with a nested list comprehension, then print it.
Output: complete Python program
Flatten a list of lists

Flatten values from objects

A flat map first takes a list out of every element, then joins all those lists together. Here the first for walks the containers and the second walks c.values. All values from the three containers end up in one list.

Prompt
Define a Container class with a values attribute that is a list of strings. Make three containers holding one, two, three; four, five; and six, seven, eight. Collect all their values into one list and print it.
Output: complete Python program
Flatten values from objects

String Representation

Often you want to show a collection as readable text, for example with commas between the elements.

In Python the separator string has the join method: ", ".join(words) returns one string. join accepts only strings, so numbers must be converted first, for example with str(n) in a comprehension. The extras — a prefix, a postfix, a limit, or a transformation — are done with plain string concatenation or a slice before joining.

Join with a separator

join puts ", " between the words and returns one string. Note that the separator comes first: it is the string you call the method on.

Prompt
Join the words one, two, three into one string with ", " between them using the join method. Print the string.
Output: complete Python program
Join with a separator

Add a prefix and a postfix

A prefix and a postfix are just text around the joined result. In Python you add them with +.

Prompt
Join the words one, two, three with " | " between them, add the prefix "start: " and the postfix ": end", and print the result.
Output: complete Python program
Add a prefix and a postfix

Limit the number of elements

For a long list you may want to show only the first few elements. The slice numbers[:10] takes the first 10, the comprehension turns each into a string with str(n), and "<...>" at the end shows that more elements were cut off.

Prompt
Make a list with the numbers from 1 to 100. Join only the first 10 of them with ", " and add "<...>" at the end to show the list was cut. Print the result.
Output: complete Python program
Limit the number of elements

Transform each element before joining

To change every element before joining, build a new list of strings with a comprehension, then call join on it. Here each word becomes "Element: " plus the word in uppercase.

Prompt
Turn each of the words one, two, three into "Element: " followed by the word in uppercase. Join the results with ", " and print the string.
Output: complete Python program
Transform each element before joining