Lists: Exercises


3. Slicing Lists. In Python, you can extract a portion of a list using slicing. Slicing is done by specifying a start and end index, separated by a colon :.

For example, items[0:2] returns a new list with items from the first to the third item in the original list.

In the exercise below, you are given a list, items. Use slicing to select the second to fourth elements.


# Define list. items = ["inflation", "unemployment", "gdp growth", "interest rates"] # Slice the list to extract the second to fourth items. selected_items = items[___:___] # Print the selected items. print(selected_items) # Define list. items = ["inflation", "unemployment", "gdp growth", "interest rates"] # Slice the list to extract the second to fourth items. selected_items = items[1:4] # Print the selected items. print(selected_items) test_object("selected_items") success_msg("Correct!")
To slice a list, use the format start_index:end_index to extract a portion of the list.

Previous Exercise Next Exercise