loading...

Charming the Python: List Comprehension

twitter logo github logo Updated on ・1 min read

30 Days of Python (19 Part Series)

1) Charming the Python: the basic basics 2) Charming the Python: Builtins and Variables 3 ... 17 3) Charming the Python: Boolean & Operators 4) Charming the Python: Strings 5) Charming the Python: Lists 6) Charming the Python: Tuples 7) Charming the Python: Sets 8) Charming the Python: Dictionaries 9) Charming the Python: Data Structures 10) Charming the Python: Conditionals 11) Charming the Python: Loops 12) Charming the Python: Functions 13) Charming the Python: Modules 14) Charming the Python: List Comprehension 15) Charm the Python: Lambdas 16) Charming the Python: Error Types 17) Charming the Python: datetime 18) Charming the Python: Exception Handling 19) Charming the Python: RegEx

If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.


List Comprehension

List comprehensions are shorthand ways to make lists from sequences. This is much faster than processing a list via a for loop.

List comprehensions alow you to make a list and define it's contents. This quicker than creating an empt list, then populating it.

# syntax
list = [i for i in iterable if expression]
dog_letters = [ letter for letter in 'dog' ]
print( dog_letters)
>>> ['d', 'o', 'g']

Math example

squares = [number * number for number in range(10)]  # multiplies 0*0, then 1*1, then 2*2, then 3*3...
print(squares)
>>> 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 

Series based on

twitter logo DISCUSS
Classic DEV Post from Apr 21 '19

Planning a Real-World Web Application & Key Considerations(Part 1: Project Management)

This series of posts document a high-level process to use when planning a modern web application, from project organization, collaboration considerations and tooling choices during development, all the way through deployment and performance strategies.

Vicki (she/her) profile image

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️