Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
Let's talk about list comprehensions in Python.
We're going to talk about, what list comprehensions are, but we're not going to talk about, why you might use them or how you might write them. For now, we'll just discuss what they do.
comprehensiondefinition in Python Terminology.
We have a list of strings (screencasts
) that Python Morsels represents screencast names:
>>> screencasts = [
... "Data structures contain pointers",
... "What is self?",
... "What is a class?",
... "Slicing",
... "How to make a function",
... "Methods are just functions attached to classes",
... ]
...
And we have a for
loop that loops over that list of strings and makes a new list of strings (names
), changing each of the strings a little bit along the way:
>>> names = []
>>> for name in screencasts:
... names.append(name.title())
...
We've title-cased these strings, so the first letter of each word is capitalized.
>>> from pprint import pprint
>>> pprint(names)
['Data Structures Contain Pointers',
'What Is Self?',
'What Is A Class?',
'Slicing',
'How To Make A Function',
'Methods Are Just Functions Attached To Classes']
That for
loop is equivalent to this list comprehension:
>>> names = [name.title() for name in screencasts]
This one line of code above is called a list comprehension.
That list comprehension does the same thing as the three lines of code in this for
loop:
>>> names = []
>>> for name in screencasts:
... names.append(name.title())
...
for
loopsList comprehensions are a more special-purpose tool than for
loops.
Python's for
loops allow you to loop over an iterable and perform pretty much any action.
But list comprehensions are specifically for looping over some existing iterable and making a new list out of it, usually while changing each element a little big along the way.
But you can also use list comprehensions for filtering elements down, only including items that match a specific condition.
This for
loop is nearly the same as the for
loop we had before:
>>> short_names = []
>>> for name in screencasts:
... if len(name) <= 30:
... short_names.append(name.title())
...
But unlike before, we're building up a new list (short_names
) that only includes names with 30 or fewer characters in them.
So the short_names
list that we end up with will have fewer strings in it than the original list of screencasts
that we started with:
>>> from pprint import pprint
>>> pprint(screencasts)
['Data structures contain pointers',
'What is self?',
'What is a class?',
'Slicing',
'How to make a function',
'Methods are just functions attached to classes']
>>> pprint(short_names)
['What Is Self?', 'What Is A Class?', 'Slicing', 'How To Make A Function']
An equivalent list comprehension will look very similar to the comprehension we had before:
>>> short_names = [name.title() for name in screencasts if len(name) <= 30]
When constructing a list comprehension, the thing we're appending to the new list (name.title()
) comes first, and then our looping logic comes after that, and then the condition (if there is one) comes last.
So this list comprehension:
>>> short_names = [name.title() for name in screencasts if len(name) <= 30]
Is equivalent to this for
loop:
>>> short_names = []
>>> for name in screencasts:
... if len(name) <= 30:
... short_names.append(name.title())
...
In Python, for
loops are general-purpose tools.
We can use a for
loop for looping over a iterable and performing one or more actions.
List comprehensions are special-purpose tools. They're specifically for taking an old iterable, looping over it, and making a new list out of it, usually while changing each element a little bit along the way, but you can also use them for filtering elements down (only including elements that match a specific condition).
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll explain concepts that new Python programmers often overlook.
In Python it's very common to build up new lists while looping over old lists. Partly this is because we don't mutate lists very often while looping over them.
Because we build up new lists from old ones so often, Python has a special syntax to help us with this very common operation: list comprehensions.
To track your progress on this Python Morsels topic trail, sign in or sign up.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.