0% found this document useful (0 votes)
62 views

Comprehension Examples - Ipynb - Colaboratory

The document discusses list comprehensions in Python and provides examples of how to use them to create lists. It shows how list comprehensions can be used to generate lists from ranges, with expressions, and with if conditions. The document also mentions set and dictionary comprehensions but does not provide examples. It focuses on the basics of list comprehensions and some common uses like generating ranges, doubling values, and filtering with conditions.

Uploaded by

Vesselin Nikov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Comprehension Examples - Ipynb - Colaboratory

The document discusses list comprehensions in Python and provides examples of how to use them to create lists. It shows how list comprehensions can be used to generate lists from ranges, with expressions, and with if conditions. The document also mentions set and dictionary comprehensions but does not provide examples. It focuses on the basics of list comprehensions and some common uses like generating ranges, doubling values, and filtering with conditions.

Uploaded by

Vesselin Nikov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

24.06.2020 г. Comprehension examples.

ipynb - Colaboratory

List Comprehension in Python: Explained with Examples

1. Create a list using a list comprehension

Loop

numbers = []
for number in range(1, 11):
numbers.append(number)

print(numbers)

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

LC

numbers = [number for number in range(1, 11)]

print(numbers)

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

2. List comprehension with an expression

Loop

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

https://colab.research.google.com/drive/1jfQohtm_cH1bGqE4jCM8HjAUTSpYm0e_#printMode=true 1/5
24.06.2020 г. Comprehension examples.ipynb - Colaboratory
doubled_numbers = []
for number in numbers:
doubled_numbers.append(number * 2)

print(doubled_numbers)

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

LC

doubled_numbers = [number * 2 for number in numbers]

print(doubled_numbers)

3. List comprehension with an if condition

Loop

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

odd_squares = []
for number in numbers:
if number % 2 == 1:
odd_squares.append(number * number)

print(odd_squares)

LC

https://colab.research.google.com/drive/1jfQohtm_cH1bGqE4jCM8HjAUTSpYm0e_#printMode=true 2/5
24.06.2020 г. Comprehension examples.ipynb - Colaboratory

odd_squares = [number * number for number in numbers if number % 2 == 1]

print(odd_squares)

4. List comprehension with an if-else condition


↳ 4 cells hidden

5. List comprehension with an if-elif-else ladder


↳ 4 cells hidden

6. Using methods within list comprehension


↳ 4 cells hidden

7. List comprehension with nested-if conditions


↳ 4 cells hidden

8. List comprehension equivalent of nested for loops


↳ 4 cells hidden

https://colab.research.google.com/drive/1jfQohtm_cH1bGqE4jCM8HjAUTSpYm0e_#printMode=true 3/5
24.06.2020 г. Comprehension examples.ipynb - Colaboratory

9. List comprehension to atten a list of lists


↳ 4 cells hidden

10. Nested list comprehension


↳ 6 cells hidden

Set Comprehensions
↳ 2 cells hidden

Dictionary Comprehensions
↳ 5 cells hidden

https://colab.research.google.com/drive/1jfQohtm_cH1bGqE4jCM8HjAUTSpYm0e_#printMode=true 4/5
24.06.2020 г. Comprehension examples.ipynb - Colaboratory

https://colab.research.google.com/drive/1jfQohtm_cH1bGqE4jCM8HjAUTSpYm0e_#printMode=true 5/5

You might also like