Question: There is actually a dictionary comprehension to create a dictionary, just as there is a list comprehension that creates a list. See if you can

There is actually a dictionary comprehension to create a dictionary, just as there is a list comprehension that creates a list.
See if you can identify the meaning of each of the comprehensions in the following code segment. You would be able to execute these statements if you copied (or cut-and-paste) them one at a time into the Shell.
word = 'Mississippi'
print([word[i] for i in range(1,len(word)) if word[i]==word[i-1]])
print(''.join([word[0]]+[word[i] for i in range(1,len(word)) if word[i]!=word[i-1]]))
1) What is the first list comprehension collecting into the list?
2) What is the second comprehension collecting into its list?
3) What is the '''.join doing to that list?
4) What earlier recitation produced results like these?
5) What is your opinion about this code as compared to that earlier recitation?
Readable/Unreadable? Clear/Confusing? Like/Dislike? Good/Bad?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!