Creating Simple Lists
Part of the Logic & Flow section of Coddy's Python journey — lesson 51 of 78.
Now that you know the syntax of list comprehensions, let’s dive into some simple examples to see how they can be used to create new lists quickly and efficiently.
Create a list of cube numbers:
cubes = [x**3 for x in range(1, 6)]
print(cubes)
# Output: [1, 8, 27, 64, 125]Create a list of strings from characters:
chars = [char for char in "hello"]
print(chars)
# Output: ['h', 'e', 'l', 'l', 'o']Generate a list of even numbers:
evens = [x for x in range(2, 11, 2)]
print(evens)
# Output: [2, 4, 6, 8, 10]Convert all items in a list to uppercase:
words = ["apple", "banana", "cherry"]
uppercase = [word.upper() for word in words]
print(uppercase)
# Output: ['APPLE', 'BANANA', 'CHERRY']List comprehensions make it easy to work with sequences, apply transformations, and even include conditions—all in one line!
Challenge
EasyCreate a function named get_word_lengths that takes a list of words as an argument and returns a list of the lengths of each word using a list comprehension.
Cheat sheet
List comprehensions provide a concise way to create new lists by applying transformations to existing sequences.
Create a list of cube numbers:
cubes = [x**3 for x in range(1, 6)]
# Output: [1, 8, 27, 64, 125]Create a list of strings from characters:
chars = [char for char in "hello"]
# Output: ['h', 'e', 'l', 'l', 'o']Generate a list of even numbers:
evens = [x for x in range(2, 11, 2)]
# Output: [2, 4, 6, 8, 10]Convert all items in a list to uppercase:
words = ["apple", "banana", "cherry"]
uppercase = [word.upper() for word in words]
# Output: ['APPLE', 'BANANA', 'CHERRY']Try it yourself
def get_word_lengths(words):
# 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