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 5Use a list comprehension with a condition to create a new list called
long_fruits that contains only fruits with length greater than min_lengthPrint 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