Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

esson 2    ❮    Lesson List    ❮    Top Page

2.1 for Loop

2.2 List Comprehension

2.3 Boolean Operators

2.4 if Statement

❯  2.5  Looping if Statement   

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   6m 6s
☷  Interactive readings   5m

Combining if Statements and for Loop

Combining lists and if statements can be very beneficial. By adjusting the indentation properly, we can easily combine both of them.

Using if with List Comprehension 

We can add a condition to the list comprehensions to modify the existing list. The for part has to be written before the if part. 

Using if-else with List Comprehension 

Adding else or multiple ifs are also possible to do in the list comprehension. In this case, the for part has to be written after the else part.

Using multiple if-else in List Comprehension 

While adding elif is not possible in list comprehension, you can always chain multiple if-else. 

However, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse