Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Picking Elements

The loop that builds a new list can also pick elements. In the lesson on loops, an if inside it decided which elements to .append(), for example only the scores of 50 points or more:

scores = [72, 45, 90, 38, 50, 64, 29, 85]
passed = []
for score in scores:
    if score >= 50:
        passed.append(score)
print(passed)
[72, 90, 50, 64, 85]

A list comprehension can pick elements too. Write the condition at the end, after the for part:

scores = [72, 45, 90, 38, 50, 64, 29, 85]
passed = [score for score in scores if score >= 50]
print(passed)
[72, 90, 50, 64, 85]

Read it as “score for each score in scores, if score >= 50”. Only the elements for which the condition is true go into the new list. The first part is just score this time, because each element goes into the new list unchanged. score for score in scores may look like the same word three times, but each has its job: what goes into the list, the name of the variable, and the list the elements come from.

You can change and pick at once: [score * 2 for score in scores if score >= 50] doubles only the scores of 50 or more. And since the result is a list, len() tells you how many elements were picked: len(passed) is 5.

The for part can be the first line of any for loop you know, also one with two variables, as in the lesson on tuples and sets. This picks the names of the players with at least 100 points:

results = [("Anna", 120), ("Tom", 95), ("Eva", 140), ("Max", 60)]
winners = [name for name, points in results if points >= 100]
print(winners)
['Anna', 'Eva']

The typical mistake is to put the if in the wrong place. [score if score >= 50 for score in scores] ends with SyntaxError: expected 'else' after 'if' expression: an if in front of the for means something else in Python, and that needs an else. The if that picks elements goes at the end, without a colon.

Instructions

  1. 1

    Only people who are at least 140 cm tall can ride the roller coaster. Make a list can_ride with a list comprehension: the heights from heights that are 140 or more. Print it: [152, 167, 145, 171].

  2. 2

    Below the list, print how many people can ride, with len(): Can ride: 4. Keep the line with heights as it is: the exercise also checks your program with other heights.

Back

2/6

  1. 1. A List in One Line
  2. 2. Picking Elements
  3. 3. Methods and Functions Inside
  4. 4. Quiz: List Comprehensions
  5. 5. Dictionary Comprehensions
  6. 6. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.