Bird
Raised Fist0
Pythonprogramming~10 mins

List creation and representation in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a list named fruits with three items: 'apple', 'banana', and 'cherry'.

Python
fruits = [1]
Drag options to blanks, or click blank then click option'
A{'apple', 'banana', 'cherry'}
B('apple', 'banana', 'cherry')
C['apple', 'banana', 'cherry']
D'apple', 'banana', 'cherry'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses instead of square brackets creates a tuple, not a list.
Using curly braces creates a set, which is unordered.
Writing items without brackets creates a syntax error.
2fill in blank
medium

Complete the code to create an empty list named numbers.

Python
numbers = [1]
Drag options to blanks, or click blank then click option'
A[]
B()
C{}
D''
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses creates an empty tuple, not a list.
Using curly braces creates an empty dictionary, not a list.
Using empty quotes creates an empty string.
3fill in blank
hard

Fix the error in the code to correctly create a list named colors with 'red', 'green', and 'blue'.

Python
colors = [1]
Drag options to blanks, or click blank then click option'
A['red', 'green', 'blue']
B{'red', 'green', 'blue'}
C('red', 'green', 'blue')
D'red', 'green', 'blue'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses instead of square brackets.
Using curly braces which create a set.
Listing items without brackets causes syntax errors.
4fill in blank
hard

Fill both blanks to create a list of squares for numbers 1 to 5 using list comprehension.

Python
squares = [x[1] for x in range(1, 6) if x [2] 3]
Drag options to blanks, or click blank then click option'
A**2
B>
C<=
D*2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using *2 instead of **2 for squaring.
Using <= instead of > in the condition.
Forgetting the exponent operator.
5fill in blank
hard

Fill all three blanks to create a dictionary where keys are uppercase words and values are their lengths, but only for words longer than 4 letters.

Python
result = { [1]: [2] for word in words if len(word) [3] 4 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word instead of word.upper() for keys.
Using word instead of len(word) for values.
Using <= or < instead of > in the condition.

Practice

(1/5)
1. Which of the following is the correct way to create an empty list in Python?
easy
A. ()
B. {}
C. []
D. list()

Solution

  1. Step 1: Understand list syntax

    In Python, lists are created using square brackets [] or the list() function.
  2. Step 2: Identify empty list creation

    [] is the literal syntax for an empty list, while list() also creates an empty list but is a function call.
  3. Final Answer:

    [] -> Option C
  4. Quick Check:

    Empty list = [] [OK]
Hint: Empty lists use square brackets [] directly [OK]
Common Mistakes:
  • Using curly braces {} which create sets or dictionaries
  • Using parentheses () which create tuples
  • Confusing list() function with empty list literal
2. Which of the following is the correct syntax to create a list with the items 1, 2, and 3?
easy
A. list = [1, 2, 3]
B. list = <1, 2, 3>
C. list = {1, 2, 3}
D. list = (1, 2, 3)

Solution

  1. Step 1: Recognize list syntax

    Lists in Python use square brackets [] to hold items in order.
  2. Step 2: Check each option

    list = [1, 2, 3] uses square brackets with items 1, 2, 3 correctly. list = (1, 2, 3) uses parentheses which create tuples. list = {1, 2, 3} uses curly braces which create sets. list = <1, 2, 3> uses invalid syntax.
  3. Final Answer:

    list = [1, 2, 3] -> Option A
  4. Quick Check:

    List with items = [1, 2, 3] [OK]
Hint: Lists use square brackets [] with commas between items [OK]
Common Mistakes:
  • Using parentheses () which create tuples
  • Using curly braces {} which create sets
  • Using invalid angle bracket syntax
3. What is the output of the following code?
my_list = [10, 'apple', 3.5]
print(my_list)
medium
A. [10, 'apple', 3.5]
B. (10, 'apple', 3.5)
C. {10, 'apple', 3.5}
D. 10, 'apple', 3.5

Solution

  1. Step 1: Understand list contents and print

    The list my_list contains an integer, a string, and a float. Lists can hold mixed types.
  2. Step 2: Print shows list with square brackets

    Printing a list shows its contents inside square brackets with commas separating items.
  3. Final Answer:

    [10, 'apple', 3.5] -> Option A
  4. Quick Check:

    Print list shows square brackets [OK]
Hint: Printed lists show square brackets and commas [OK]
Common Mistakes:
  • Confusing list output with tuple or set syntax
  • Expecting output without brackets
  • Misreading quotes around strings
4. The following code is intended to create a list with numbers 1 to 3, but it causes an error. What is the problem?
numbers = [1, 2, 3
print(numbers)
medium
A. Using quotes around numbers
B. Using parentheses instead of square brackets
C. Missing commas between numbers
D. Missing closing square bracket ]

Solution

  1. Step 1: Check list syntax

    The list starts with [ but does not have a closing ] bracket.
  2. Step 2: Identify syntax error

    Missing the closing bracket causes a syntax error when Python tries to parse the list.
  3. Final Answer:

    Missing closing square bracket ] -> Option D
  4. Quick Check:

    Lists need matching brackets [OK]
Hint: Always match opening and closing brackets [] [OK]
Common Mistakes:
  • Forgetting to close the list with ]
  • Confusing brackets with parentheses
  • Missing commas between items
5. You want to create a list that contains the numbers 1 to 5, but only even numbers should be included. Which code correctly creates this list?
hard
A. evens = [x for x in range(1, 6) if x % 2 != 0]
B. evens = [x for x in range(1, 6) if x % 2 == 0]
C. evens = [x for x in range(1, 5) if x % 2 == 1]
D. evens = [x for x in range(2, 7) if x % 2 == 1]

Solution

  1. Step 1: Understand list comprehension with condition

    The code uses a list comprehension to select numbers from 1 to 5 (range(1, 6)) and filters only even numbers where x % 2 == 0.
  2. Step 2: Check each option's condition and range

    evens = [x for x in range(1, 6) if x % 2 == 0] correctly uses range 1 to 5 and selects even numbers. Other options select odd numbers or wrong ranges.
  3. Final Answer:

    evens = [x for x in range(1, 6) if x % 2 == 0] -> Option B
  4. Quick Check:

    Even numbers filtered with x % 2 == 0 [OK]
Hint: Use list comprehension with if condition for filtering [OK]
Common Mistakes:
  • Using wrong range limits
  • Filtering odd numbers instead of even
  • Confusing modulo condition