Using List Comprehensions Effectively

Tutorial 3 of 5

Using List Comprehensions Effectively

1. Introduction

This tutorial aims to provide a comprehensive understanding of list comprehensions in Python and to demonstrate how to use them effectively. By the end of this tutorial, you will be able to understand the syntax and functionality of list comprehensions and use them to create efficient and readable Python code.

Prerequisites: Basic knowledge of Python programming (variables, data types, loops, and functions).

2. Step-by-Step Guide

Python list comprehension is a syntactic construct that enables the creation of lists in a more human-readable and efficient way. It can be used to replace traditional for loops and lambda function with map(), filter() and reduce().

Syntax of List Comprehension:

[expression for item in list if condition]

2.1 Without List Comprehensions

Without list comprehension, generating a list of squares for the first 10 natural numbers would look like this:

squares = []
for i in range(1, 11):
    squares.append(i**2)
print(squares)

2.2 With List Comprehensions

Here is how you can do the same thing with list comprehension, in a more Pythonic and efficient way:

squares = [i**2 for i in range(1, 11)]
print(squares)

2.3 Conditionals in List Comprehensions

You can also use conditionals in list comprehension to filter out certain values:

even_squares = [i**2 for i in range(1, 11) if i % 2 == 0]
print(even_squares)

3. Code Examples

Let's look at some more practical examples.

3.1 Example 1: Convert Celsius to Fahrenheit

celsius = [0, 10, 20.1, 34.5]

fahrenheit = [((9/5)*temp + 32) for temp in celsius ]

print(fahrenheit)
# Output: [32.0, 50.0, 68.18, 94.1]

3.2 Example 2: Flatten a Nested List

nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

flattened_list = [num for sublist in nested_list for num in sublist]

print(flattened_list)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

4. Summary

In this tutorial, we learned about Python's list comprehension, understood its syntax, and learned its application through examples. List comprehensions provide an efficient way to create and manipulate lists in Python. They make the code more Pythonic and maintainable.

To further enhance your understanding, you can practice using list comprehensions in your Python projects and learn more from the official Python documentation on list comprehensions.

5. Practice Exercises

  1. Exercise 1: Create a list of the first 10 Fibonacci numbers using list comprehension.

  2. Exercise 2: Use list comprehension to create a list of odd numbers between 1 and 20.

  3. Exercise 3: Use list comprehension to create a list of strings which are not palindrome from the given list of strings.

Solutions:

  1. Solution 1:
fibonacci = [0, 1]
[fibonacci.append(fibonacci[-2] + fibonacci[-1]) for _ in range(8)]
print(fibonacci)
# Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
  1. Solution 2:
odd_numbers = [i for i in range(1, 20) if i % 2 != 0]
print(odd_numbers)
# Output: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
  1. Solution 3:
list_of_strings = ["radar", "python", "madam", "hello", "world", "pop"]
not_palindromes = [word for word in list_of_strings if word != word[::-1]]
print(not_palindromes)
# Output: ['python', 'hello', 'world']