Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create 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 here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow