0
0
Pythonprogramming~15 mins

List comprehension with condition in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
List comprehension with condition
๐Ÿ“– Scenario: You are working in a fruit store. You have a list of fruit names, but you only want to select the fruits that have more than 5 letters in their names.
๐ŸŽฏ Goal: Build a Python program that uses list comprehension with a condition to create a new list containing only the fruits with names longer than 5 letters.
๐Ÿ“‹ What You'll Learn
Create a list called fruits with the exact values: 'apple', 'banana', 'kiwi', 'mango', 'pineapple', 'pear'
Create a variable called min_length and set it to 5
Use a list comprehension with a condition to create a new list called long_fruits that contains only fruits with length greater than min_length
Print the long_fruits list
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Filtering lists based on conditions is common in data processing, like selecting products, names, or records that meet certain criteria.
๐Ÿ’ผ Career
Understanding list comprehension with conditions helps in writing clean and efficient code for data analysis, web development, and automation tasks.
Progress0 / 4 steps
1
Create the initial list of fruits
Create a list called fruits with these exact values: 'apple', 'banana', 'kiwi', 'mango', 'pineapple', 'pear'
Python
Need a hint?

Use square brackets [] to create a list and separate items with commas.

2
Set the minimum length for fruit names
Create a variable called min_length and set it to the number 5
Python
Need a hint?

Use the equals sign = to assign the value 5 to the variable min_length.

3
Use list comprehension with condition
Use a list comprehension with a condition to create a new list called long_fruits that contains only fruits from fruits whose length is greater than min_length
Python
Need a hint?

Use the syntax: [item for item in list if condition] to filter items.

4
Print the filtered list
Write a print statement to display the long_fruits list
Python
Need a hint?

Use print(long_fruits) to show the result.