Lists help you keep many items together in one place. You can easily add, remove, or look at items in a list.
List creation and representation in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
my_list = [item1, item2, item3] # Example: numbers = [1, 2, 3, 4] words = ['apple', 'banana', 'cherry']
Lists are written inside square brackets [].
Items inside a list can be of any type: numbers, words, or even mixed.
Examples
Python
empty_list = []
print(empty_list)Python
single_item_list = [42] print(single_item_list)
Python
mixed_list = [1, 'hello', 3.14, True] print(mixed_list)
Python
nested_list = [[1, 2], [3, 4]] print(nested_list)
Sample Program
This program shows how to create a list, add an item, and print items from it.
Python
def main(): # Create a list of fruits fruits = ['apple', 'banana', 'cherry'] print('Original list:', fruits) # Add a new fruit fruits.append('date') print('After adding date:', fruits) # Show the first fruit print('First fruit:', fruits[0]) # Show the whole list print('Complete list:', fruits) if __name__ == '__main__': main()
Important Notes
Creating a list is very fast and uses space proportional to the number of items.
Lists keep the order of items, so the first item you add stays first unless you change it.
Common mistake: forgetting the square brackets and trying to create a list like my_list = 1, 2, 3 which creates a tuple, not a list.
Summary
Lists store multiple items in one variable using square brackets.
You can create empty lists, single-item lists, or lists with many items.
Lists keep the order of items and can hold different types together.
Practice
1. Which of the following is the correct way to create an empty list in Python?
easy
Solution
Step 1: Understand list syntax
In Python, lists are created using square brackets[]or thelist()function.Step 2: Identify empty list creation
[]is the literal syntax for an empty list, whilelist()also creates an empty list but is a function call.Final Answer:
[] -> Option CQuick 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
Solution
Step 1: Recognize list syntax
Lists in Python use square brackets[]to hold items in order.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.Final Answer:
list = [1, 2, 3] -> Option AQuick 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
Solution
Step 1: Understand list contents and print
The listmy_listcontains an integer, a string, and a float. Lists can hold mixed types.Step 2: Print shows list with square brackets
Printing a list shows its contents inside square brackets with commas separating items.Final Answer:
[10, 'apple', 3.5] -> Option AQuick 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
Solution
Step 1: Check list syntax
The list starts with[but does not have a closing]bracket.Step 2: Identify syntax error
Missing the closing bracket causes a syntax error when Python tries to parse the list.Final Answer:
Missing closing square bracket ] -> Option DQuick 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
Solution
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 wherex % 2 == 0.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.Final Answer:
evens = [x for x in range(1, 6) if x % 2 == 0] -> Option BQuick 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
