0%

Lists · practice

Measure, Search, and Slice Lists

You already know several operations that work with . are also ordered sequences, so some familiar ideas carry over.

Measure and check membership

len() counts list items. The in reports whether a is present:

Try it

The membership checks produce values, so they can later become in an statement.

Take a slice

A list uses a start and a stop index. It includes the start and stops before the stop index:

Try it

The slice contains Oslo, Kyoto, and Lima. Like a string slice, a list slice creates a new value.

What does this code print?

letters = ["a", "b", "c", "d"]
print("c" in letters)
print(letters[1:3])

In the exercise, measure one list, check for one item, and take its middle slice. Length and slice boundaries transfer from strings. The new in operator adds a way to ask whether the list contains a value.

Task

Complete the program using the existing destinations :

  • Store its item count in count.

  • Store whether "Kyoto" is present in has_kyoto.

  • Store the from 1 up to but not including index 4 in middle.

  • Print count, has_kyoto, and middle, one per line.