Get Started

Loop over list of lists

Remember the house variable from the Intro to Python course? Have a look at its definition in the script. It's basically a list of lists, where each sublist contains the name and area of a room in your house.

It's up to you to build a for loop from scratch this time!

This is a part of the course

“Intermediate Python”

View Course

Exercise instructions

Write a for loop that goes through each sublist of house and prints out the x is y sqm, where x is the name of the room and y is the area of the room.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# house list of lists
house = [["hallway", 11.25], 
         ["kitchen", 18.0], 
         ["living room", 20.0], 
         ["bedroom", 10.75], 
         ["bathroom", 9.50]]
         
# Build a for loop from scratch

This exercise is part of the course

Intermediate Python

BeginnerSkill Level
4.6+
772 reviews

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

There are several techniques you can use to repeatedly execute Python code. While loops are like repeated if statements, the for loop iterates over all kinds of data structures. Learn all about them in this chapter.

Exercise 1: while loopExercise 2: while: warming upExercise 3: Basic while loopExercise 4: Add conditionalsExercise 5: for loopExercise 6: Loop over a listExercise 7: Indexes and values (1)Exercise 8: Indexes and values (2)Exercise 9: Loop over list of lists
Exercise 10: Loop Data Structures Part 1Exercise 11: Loop over dictionaryExercise 12: Loop over NumPy arrayExercise 13: Loop Data Structures Part 2Exercise 14: Loop over DataFrame (1)Exercise 15: Loop over DataFrame (2)Exercise 16: Add column (1)Exercise 17: Add column (2)

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free