The Syntax
Part of the Logic & Flow section of Coddy's Python journey — lesson 50 of 78.
A list comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. The basic syntax of a list comprehension is:
new_list = [expression for item in iterable]Here's a breakdown of the syntax:
new_list: The new list that will be created.expression: The value to be included innew_list. This can be a modified version ofitem.
for item in iterable: A loop that iterates over each element in theiterable(e.g., a list, tuple, or range).item: The current element being processed in the loop.
Here's a simple example:
numbers = [1, 2, 3, 4, 5]
squares = [n * n for n in numbers]
print(squares)
# Output: [1, 4, 9, 16, 25]In this example, the list comprehension creates a new list called squares by taking each element n from the numbers list and squaring it.
Challenge
EasyCreate a function named double_numbers that takes a list of numbers numbers as an argument. The function should use a list comprehension to create a new list where each number in the original list is doubled. The function should return the new list.
Cheat sheet
A list comprehension offers a shorter syntax to create a new list based on values of an existing list:
new_list = [expression for item in iterable]Where:
expression: The value to be included in the new listitem: The current element being processediterable: The original list, tuple, or range
Example:
numbers = [1, 2, 3, 4, 5]
squares = [n * n for n in numbers]
print(squares)
# Output: [1, 4, 9, 16, 25]Try it yourself
def double_numbers(numbers):
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker10Basic List Comprehensions
The SyntaxCreating Simple ListsAdding ConditionsUsing Data AggregationRecap - House Of ListsRecap - Elements Of Freedom2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter