Python Tutorial

Lists and Arrays

The foundational linear structure: contiguous, index-addressable storage for a sequence of elements.

Arrays vs Python Lists

A classic array is a fixed-size block of memory holding elements of one type, addressed by index. A Python list is a dynamic array: it grows automatically and can hold mixed types. Both give O(1) access by index because the address of element i is computed directly.

Core Operations and Their Cost

OperationExampleTime
Access by indexa[i]O(1)
Update by indexa[i] = xO(1)
Appenda.append(x)O(1) amortized
Insert / delete at fronta.insert(0, x)O(n)
Search by valuex in aO(n)
nums = [10, 20, 30, 40]

print(nums[2])          # 30  -> O(1)
nums.append(50)         # [10,20,30,40,50] -> amortized O(1)
nums.insert(0, 5)       # shifts everything right -> O(n)
nums.remove(30)         # find + shift -> O(n)
print(nums)             # [5, 10, 20, 40, 50]

Why Append Is "Amortized" O(1)

A dynamic array over-allocates memory. Most appends just drop the value into a free slot (O(1)). Occasionally the array is full and must be copied to a larger block (O(n)), but that cost is spread across many cheap appends, averaging out to O(1).

Traversal and Common Patterns

nums = [4, 8, 15, 16, 23, 42]

# find max in one pass -> O(n)
largest = nums[0]
for x in nums[1:]:
    if x > largest:
        largest = x
print(largest)          # 42

# two-pointer reverse in place -> O(n), O(1) extra space
i, j = 0, len(nums) - 1
while i < j:
    nums[i], nums[j] = nums[j], nums[i]
    i += 1
    j -= 1
print(nums)             # [42, 23, 16, 15, 8, 4]

Typed Arrays and NumPy

When you need many numbers stored compactly, Python's array module or NumPy arrays use far less memory than a list of Python objects and run vectorized operations in C speed.

from array import array
scores = array("i", [90, 85, 77])   # 'i' = signed int, one type only

import numpy as np
a = np.array([1, 2, 3, 4])
print((a * 2).sum())    # 20, computed in a fast C loop

2-D Arrays (Matrices)

grid = [[1, 2, 3],
        [4, 5, 6]]

print(grid[1][2])       # 6

# WRONG: [[0]*3]*2 makes 2 references to the SAME row
# RIGHT: build independent rows
matrix = [[0] * 3 for _ in range(2)]
matrix[0][0] = 9
print(matrix)           # [[9, 0, 0], [0, 0, 0]]

[[0]*3]*2 is a classic bug: all rows point to one list, so editing one edits all. Use a comprehension instead.

Best Practices

  • Use a list for ordered, index-based data; a set/dict when you need fast membership.
  • Avoid inserting/deleting at the front of a big list — use collections.deque.
  • Use NumPy for large numeric arrays and math.
  • Build 2-D lists with comprehensions to keep rows independent.

Try It Yourself

Exercise 1: Reverse a list in place using two pointers (no reverse()).

Show solution
a = [1, 2, 3, 4, 5]
i, j = 0, len(a) - 1
while i < j:
    a[i], a[j] = a[j], a[i]
    i += 1; j -= 1
print(a)   # [5, 4, 3, 2, 1]

Exercise 2: Why is inserting at the front of a list O(n)?

Show solution

Every existing element must shift one position to the right to make room.

📘 Real-World Deep Dive

Knowing <strong>DSA Lists Arrays (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Lists Arrays that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

class DynArray:
    def __init__(self, capacity=4):
        self.cap, self.size, self.data = capacity, 0, [None] * capacity
    def push(self, v):
        if self.size == self.cap:
            self.cap *= 2
            self.data += [None] * self.cap
        self.data[self.size] = v; self.size += 1
    def __repr__(self):
        return repr(self.data[:self.size])

a = DynArray()
for i in range(6): a.push(i)
print(a)

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Lists Arrays as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Lists and Arrays

Common questions about this page.

What is Lists and Arrays?

Lists and Arrays is a DSA lesson that explains lists and arrays in Python. The foundational linear structure: contiguous, index-addressable storage for a sequence of elements. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run lists and arrays examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn lists and arrays in this DSA Python lesson (Lists and Arrays).

How do I use lists and arrays in Python?

To use lists and arrays in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of lists and arrays?

This Lists and Arrays tutorial shows lists and arrays syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Lists and Arrays example for beginners

Yes. This page includes a beginner lists and arrays example you can copy and run. It is designed for searches such as "lists and arrays for beginners", "lists and arrays example", and "how to use lists and arrays".

What are common mistakes with lists and arrays?

Common lists and arrays mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn lists and arrays?

Lists and Arrays is used in real Python work. Learning lists and arrays helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Lists and Arrays free to learn online?

Yes. You can learn lists and arrays free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.