Recommended resources
The freely available resources this lesson will direct you to.
Hello! Last lesson, you moved a conversion calculation into a function so the caller could use its returned result. So far, though, the converter handles one distance at a time and uses separate variables for its choices.
In this lesson, you’ll use a list to hold several distances and a dictionary to associate unit names with conversion factors. Then you’ll combine both structures with your function to convert a batch of measurements. Allow about 40–45 minutes, including the videos and time to run the code.
Lists: one variable, several values
Suppose you have three distances to convert. You could name three variables, but every additional distance would require another name and another calculation. A list lets you keep the distances together and process them with a loop.
Watch the list portions of Python Tutorial for Beginners 4: Lists, Tuples, and Sets by Corey Schafer. The full video later moves into other collection types; you only need the indicated portions today.
Python Tutorial for Beginners 4: Lists, Tuples, and Sets
Corey Schafer’s list examples show how to create a collection, retrieve its items, add an item, and loop over the collection.
Watch creating lists for square brackets and len(), then indexes and slices. In the section on adding items, watch append and insert; notice that inserting shifts existing items rather than replacing them. Skip ahead to looping through items and focus on how the loop variable receives one list value at a time.
Create lists_lab.py in your VS Code project. Save it and run it with Run Python File:
distances_m = [1.2, 0.5, 2.0]
print(distances_m[0]) # 1.2: first item
print(distances_m[-1]) # 2.0: last item
print(distances_m[:2]) # [1.2, 0.5]: first two items
print(len(distances_m)) # 3: number of items
A list uses square brackets, with commas between items. It preserves their order. Its index starts at 0, so distances_m[0] is the first distance. A negative index counts back from the end. A slice such as distances_m[:2] produces a new list containing items before index 2; it does not include the item at index 2.

Lists can change after you create them. Add these lines to lists_lab.py and run it again:
distances_m[1] = 0.75
distances_m.append(3.0)
print(distances_m) # [1.2, 0.75, 2.0, 3.0]
distances_m.remove(3.0)
print(distances_m) # [1.2, 0.75, 2.0]
Assignment at index 1 replaces an item. append() adds one item at the end. remove(3.0) removes the first item whose value is 3.0; it does not mean “remove index 3.” These methods change the existing list. In particular, don’t write distances_m = distances_m.append(3.0): append() changes the list but returns None, not the updated list.
Transform a whole list with a loop
You already know how to multiply one distance by 1000. A loop applies that calculation to every item, and a second list collects the answers. Add this below your existing code in lists_lab.py:
converted_mm = []
for distance_m in distances_m:
converted_mm.append(distance_m * 1000)
print(converted_mm) # [1200.0, 750.0, 2000.0]
print(distances_m) # [1.2, 0.75, 2.0]
Read the loop as: “For each distance in distances_m, calculate its millimetre value and append that value to converted_mm.” You do not need an index because the calculation uses each value directly. The original distances remain in metres; the new list contains the converted values in the same order. That distinction matters when you later need both the input and the result.
A useful rule is to choose a list when you have a sequence of similar items—measurements, tasks, or results—and intend to visit them in order. But what if you need to look up a conversion factor by a name such as "mm"? That is a different kind of relationship.
Dictionaries: look up a value by its label
A dictionary stores key–value pairs. For the converter, "mm" can be a key and 1000 its value. Instead of remembering which numbered position holds the millimetre factor, you ask for the factor associated with "mm".
Watch the selected portions of Python Tutorial for Beginners 5: Dictionaries - Working with Key-Value Pairs, also by Corey Schafer.
Python Tutorial for Beginners 5: Dictionaries - Working with Key-Value Pairs
Corey Schafer demonstrates dictionary creation, key-based lookup, missing-key behavior, updates, and iteration.
Watch key–value pairs and creating dictionaries. Continue through missing keys, paying particular attention to the difference between square-bracket lookup and .get(). Watch adding and updating, then skip to looping over pairs to see what .items() supplies to a loop.
Create dictionaries_lab.py:
factors = {"mm": 1000, "cm": 100}
print(factors["mm"]) # 1000
print(factors.get("km", "unsupported")) # unsupported
factors["m"] = 1
for unit, factor in factors.items():
print(unit, factor)
Curly brackets create the dictionary; each colon separates a key from its value. factors["mm"] retrieves the value for that key. factors["m"] = 1 adds a new pair; assigning to a key that already exists would replace its value rather than create a second copy of the key.
What if a key is absent? factors["km"] would raise a KeyError. .get("km", "unsupported") instead returns the supplied fallback. A word such as "unsupported" is useful for a message, not as a factor to multiply by. Before performing a conversion, check that the requested unit is a key:
requested_unit = "km"
if requested_unit in factors:
print("Factor:", factors[requested_unit])
else:
print("Unsupported unit.")
Finally, factors.items() lets the loop receive a key and its associated value together. A dictionary is a good fit when values have meaningful lookup labels. Although modern Python dictionaries retain insertion order, they are not indexed like lists: factors[0] does not mean “the first factor.”
Combine the structures in a small batch converter
You now have three pieces: your previous scale_distance() function, a list of distances, and a dictionary of factors. Create batch_converter.py:
def scale_distance(distance_m, factor):
return distance_m * factor
def convert_batch(distances_m, target_unit, factors):
results = []
for distance_m in distances_m:
converted = scale_distance(distance_m, factors[target_unit])
results.append({
"metres": distance_m,
"converted": converted,
"unit": target_unit,
})
return results
distances_m = [1.2, 0.5, 2.0]
factors = {"mm": 1000, "cm": 100}
target_unit = input("Target unit (mm or cm): ").strip().lower()
if target_unit in factors:
results = convert_batch(distances_m, target_unit, factors)
for row in results:
print(row["metres"], "m =", row["converted"], row["unit"])
else:
print("Choose mm or cm.")
Run it with mm. The displayed conversions should be 1200.0 mm, 500.0 mm, and 2000.0 mm. Run it again with cm; the corresponding values should be 120.0 cm, 50.0 cm, and 200.0 cm.
Notice the shape of results: it is a list of dictionaries. The list keeps the result rows in measurement order. Each dictionary keeps one measurement’s original distance, converted value, and unit together under descriptive keys. In the final loop, row["converted"] retrieves a field from the current row.
The if target_unit in factors check is important: convert_batch() looks up factors[target_unit], so it should only be called here after that key has been confirmed. This program still uses distances written directly in the file; the Week 1 converter project will bring these pieces back into a fuller command-line tool.
Key takeaways
Use a list for an ordered group of items, access items by index, and use a loop to build a transformed list. Use a dictionary when you need to associate keys with values and look them up by meaningful labels. Both can be changed, and they can work together: your batch converter stores a sequence of result records, each with named fields.
Next, you’ll read Python tracebacks to identify an error and test a correction—useful when an index or dictionary key is not what your program expected.