SoftwareTime221
SoftwareTime221

Reputation: 1

Stuck with List Comprehension

word = "There are seven wonders of the world"
x = word.split()
new = [len(i) for i in x if len(i)>3]
print(new)

>>> [5, 5, 7, 5]

This snippet prints a list of lengths of the words in the variable word whose len is greater than 3, BUT I also want to print the words along with the list of lengths, HOW?

Upvotes: 0

Views: 89

Answers (1)

user9706
user9706

Reputation:

You could generate a tuple (i, len(i)) instead of just len(i):

word = "There are seven wonders of the world"
x = word.split()
new = [(i, len(i)) for i in x if len(i)>3]
print(new)

which would give you the output:

[('There', 5), ('seven', 5), ('wonders', 7), ('world', 5)]

Upvotes: 1

Related Questions