List creation and representation in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create and show lists in Python, it is important to understand how the time needed grows as the list gets bigger.
We want to know how the work changes when the list size changes.
Analyze the time complexity of the following code snippet.
my_list = []
for i in range(n):
my_list.append(i)
print(my_list)
This code creates a list by adding numbers from 0 up to n-1, then prints the whole list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding an item to the list inside the loop.
- How many times: Exactly n times, once for each number from 0 to n-1.
As n grows, the number of times we add items grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the size of the list. Double the size, double the work.
Time Complexity: O(n)
This means the time to create and print the list grows in a straight line with the number of items.
[X] Wrong: "Adding items to a list inside a loop is instant and does not depend on list size."
[OK] Correct: Each addition happens once per item, so more items mean more additions and more time.
Understanding how list creation time grows helps you explain and predict performance in real coding tasks.
"What if we used list comprehension instead of a loop with append? How would the time complexity change?"
Practice
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]
- Using curly braces {} which create sets or dictionaries
- Using parentheses () which create tuples
- Confusing list() function with empty list literal
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]
- Using parentheses () which create tuples
- Using curly braces {} which create sets
- Using invalid angle bracket syntax
my_list = [10, 'apple', 3.5] print(my_list)
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]
- Confusing list output with tuple or set syntax
- Expecting output without brackets
- Misreading quotes around strings
numbers = [1, 2, 3 print(numbers)
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]
- Forgetting to close the list with ]
- Confusing brackets with parentheses
- Missing commas between items
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]
- Using wrong range limits
- Filtering odd numbers instead of even
- Confusing modulo condition
