Get Started

Slicing and dicing

Selecting single values from a list is just one part of the story. It's also possible to slice your list, which means selecting multiple elements from your list. Use the following syntax:

my_list[start:end]

The start index will be included, while the end index is not. However, it's also possible not to specify these indexes. If you don't specify the start index, Python figures out that you want to start your slice at the beginning of your list.

This is a part of the course

“Introduction to Python”

View Course

Exercise instructions

  • Use slicing to create a list, downstairs, that contains the first 6 elements of areas.
  • Create upstairs, as the last 4 elements of areas. This time, simplify the slicing by omitting the end index.
  • Print both downstairs and upstairs using print().

Hands-on interactive exercise

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

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Use slicing to create downstairs
downstairs = areas[____]

# Use slicing to create upstairs
upstairs = areas[____]

# Print out downstairs and upstairs
____
____

This exercise is part of the course

Introduction to Python

BeginnerSkill Level
4.8+
2411 reviews

Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.

Learn to store, access, and manipulate data in lists: the first step toward efficiently working with huge amounts of data.

Exercise 1: Python ListsExercise 2: Create a listExercise 3: Create lists with different typesExercise 4: List of listsExercise 5: Subsetting ListsExercise 6: Subset and conquerExercise 7: Slicing and dicing
Exercise 8: Subsetting lists of listsExercise 9: Manipulating ListsExercise 10: Replace list elementsExercise 11: Extend a listExercise 12: Delete list elementsExercise 13: Inner workings of lists

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