Comprehensions are another Python goodie that lets you write code in the functional programming paradigm. I am only going to talk about them here very lightly as functional programming is a bit beyond this tutorial. We will see them later in the solutions section.
A comprehension is another construct that allows you to loop over an iterable object. Specifically, it lets you iterate over lists, dictionaries, and sets which we have not talked about nor will we in this particular tutorial.
Comprehensions are a way to write Pythonic code. You can use comprehensions to take a lot of complex logic and reduce it to a line of code. Be aware that the return datatype of a comprehension is a list.
[[function]for x in[list,dict,orset]]
Example #1 A Different Kind Of Loop Redux
In this example we are going to take example #2 from lesson 17 and rewrite it as a comprehension.