-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path076.Challenge1.py
More file actions
29 lines (23 loc) · 863 Bytes
/
076.Challenge1.py
File metadata and controls
29 lines (23 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Rewrite the following code to use a list comprehension, instead of a for loop.
#
# Add your solution below the loop, so that the resulting list is printed out
# below output - that makes it easier to check that it's producing exactly
# the same list (and avoids entering the input text twice).
text = input("Please enter your text: ")
output = []
for x in text.split():
output.append(len(x))
print(output)
# type your solution here:
answer = [len(x) for x in text.split(' ')]
print(answer)
# It could be useful to store the original words in the list, as well.
# The for loop would look like this (note the extra parentheses, so
# that we get tuples in the list):
output = []
for x in text.split():
output.append((x, len(x)))
print(output)
# type the corresponding comprehension here:
answer = [(x, len(x)) for x in text.split(' ')]
print(answer)