In some cases, we would like to process only specific elements. For instance, we would like to take only strings that have lengths greater than 5 and make them uppercase. To filter only the strings that have lengths greater than 5, we can do that in list comprehension using an
locations = ['Maldives', 'Bali', 'Thailand', 'Egypt'] upper = [place.upper() for place in locations if len(place) > 5] print(upper) # ['MALDIVES', 'THAILAND']
This way we only took the places with lengths greater than 5 and created a new list
Given several numbers on a single line, you are asked to calculate the cube of the even ones and ignore the rest.
The input contains a single line with several integers separated by a space.
The program should output the cubes of the even numbers from the input.
Input 2 3 9 4 0 Output 8 64 0