Consider the following list of activities.
activities = ['running', 'jumping', 'shouting', 'dancing', 'singing', 'eating', 'reading']
In this exercise, we will:
ing
from the end of every activity-
to each activity|
To start, I have added a simple comprehension that just copies the list.
for a in activities
, it returns a
# The following list comprehension just returns every activity in the list of activities
[a for a in activities]
Modify the comprehension so it returns every activity with the last three letters removed. You'll want to get:
['runn', 'jump', 'shout', 'danc', 'sing', 'eat', 'read']
# Copy comprehension above, modify to remove last three letters
Answer: [a[:-3] for a in activities]
Now, add a dash after each element in the list to get:
['runn-', 'jump-', 'shout-', 'danc-', 'sing-', 'eat-', 'read-']
# Copy comprehension above, modify to add a dash
Answer: [a[:-3]+"-" for a in activities]
Now, sort the list to get:
['danc-', 'eat-', 'jump-', 'read-', 'runn-', 'shout-', 'sing-']
# Copy comprehension above, sort it
Answer: sorted([a[:-3]+"-" for a in activities])
Now, merge the list back into a string to get:
'danc- | eat- | jump- | read- | runn- | shout- | sing-'
# Copy comprehension above, join with a pipe character '|'
Answer: ' | '.join(sorted([a[:-3]+"-" for a in activities]))
The following exercise is taken from a short poem, "The Fourth", written by Shel Silverstein.
fourth = ["Oh", "CRASH!", "my", "BASH!", "it’s", "BANG!", "the", "ZANG!", "Fourth", "WHOOSH!", "of", "BAROOM!", "July", "WHEW!"]
print("\n".join(fourth))
Let's write a list comprehension to:
To start, I have added a simple comprehension that just copies all the words in the list.
# The following comprehension lists all the words in the fourth of July poem
[w for w in fourth]
Modify the list comprehension to list only words without an exclamation point.
You will need to use an if
filter. The result should be:
['Oh', 'my', 'it’s', 'the', 'Fourth', 'of', 'July']
# Modify the list comprehension above, to list only words without an exclamation point
Answer: [w for w in fourth if "!" not in w]
Join the result into a single sentence, using spaces as the join character. The result should be:
'Oh my it’s the Fourth of July'
# Modify the list comprehension above, joining on spaces to create a single message
Answer: " ".join([w for w in fourth if "!" not in w])
Now, write a list comprehension to list only words that do include an exclamation point. The answer should be:
['CRASH!', 'BASH!', 'BANG!', 'ZANG!', 'WHOOSH!', 'BAROOM!', 'WHEW!']
# Write a list comprehension to list only words that include an exclamation point
Answer: [w for w in fourth if "!" in w]
Now, edit the list comprehension above to ensure the words are all lowercase, to get:
['crash!', 'bash!', 'bang!', 'zang!', 'whoosh!', 'baroom!', 'whew!']
# Modify the list comprehension above, to make the words lowercase
Answer: [w.lower() for w in fourth if "!" in w]
Finally, edit the comprehension above to remove the exclamation point. You should end with:
['crash', 'bash', 'bang', 'zang', 'whoosh', 'baroom', 'whew']
# And remove the exclamation point
Answer: [w.lower()[:-1] for w in fourth if "!" in w]