liveBook · Manning

Data transformation

As explained in Python How-To

A data transformation is an operation or function that maps input data or its representation to modified data or a different representation.

Transform iterable data by converting its items or reshaping them into another container. In Python, map applies a function to each item and returns a map iterator; constructors such as list and dict turn iterables into useful data structures. section 5.1.3 section 7.2.2

Choose a pattern to match the transformation:

  • Use map(float, numbers_str) to convert each numeric string; wrap it in list(...) when a list is needed. For list output, a list comprehension is presented as the more Pythonic alternative. section 5.1.3 section 7.2.2
  • Use zip to pair corresponding items from two iterables, then dict to build key-value entries. The order of the iterables determines the order of each pair. section 5.1.3
numbers_str = ["1.23", "4.56", "7.89"]
 
numbers_float = list(map(float, numbers_str))
 
assert numbers_float == [1.23, 4.56, 7.89]

The conversion produces [1.23, 4.56, 7.89] from strings using map and list. section 5.1.3

zipped_tasks = dict(zip(id_numbers, titles))
 
assert zipped_tasks == {101: "Laundry", 102: "Homework", 103: "Soccer"}

The example pairs task IDs with titles to create a dictionary. section 5.1.3

How Python How-To covers it

How deep it goes
Explains map iterators, mapping functions, container construction, and paired zip items; distinguishes map as a class but does not detail its internals.
Examples
Converts numeric strings to floats, pairs task IDs with titles, and constructs lists, tuples, dictionaries, and sets from iterables.
What you can do after
Apply Python’s map, zip, and container constructors to convert iterable values or build structures from paired data.
How it connects
Connects Python iterables and container constructors with map, higher-order functions, list comprehensions, and zip.

Also explained in

Compare how 20 books explain data transformation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.