Skip to main content

Command Palette

Search for a command to run...

Arrays and Tuple in Python

Understanding Static vs Dynamic Arrays in Python

Published
β€’4 min read
Arrays and Tuple in Python

An array is a continuous block of memory used to store similar or structured data.

Arrays allow easy and fast access to elements using an index, which makes them one of the most important data structures in DSA.

In Python, we don’t have traditional arrays like in C++ or Java.
Instead, we use something called a List.

Python Lists (Dynamic Arrays)

In Python, a list is a dynamic array.

Example:

arr = [1, 2, 3, 4, 5]

Key Characteristics:

  • Lists are mutable β†’ They can be changed after declaration.

  • They are dynamic β†’ Their size can grow or shrink.

  • They allow index-based access β†’ arr[0], arr[1], etc.


Static vs Dynamic Arrays

πŸ”Ή Static Array

  • Size is fixed.

  • Cannot grow after creation.

  • Example: Arrays in C++

πŸ”Ή Dynamic Array (Python List)

  • Size can increase or decrease.

  • Memory is managed automatically.

  • Used in almost all DSA problems in Python.


Important List Operations

1️⃣ Access by Index β†’ O(1)

arr[2]

Accessing an element using an index is constant time.


2️⃣ Searching for an Element β†’ O(N)

if 5 in arr:
    print(True)

This takes O(N) time because Python needs to traverse the list element by element.


3️⃣ Important Dynamic Array Methods

βœ… append() β†’ O(1) (Amortized)

arr.append(6)
  • Adds an element to the end.

  • Amortized O(1) means most of the time it is constant time.

  • Occasionally, resizing happens internally, which takes O(N), but overall average remains O(1).


βœ… pop() β†’ O(1)

arr.pop()
  • Removes the last element.

  • Constant time operation.

⚠️ pop(index) is O(N) because shifting is required.


βœ… insert(index, value) β†’ O(N)

arr.insert(2, 10)
  • Inserts element at a specific position.

  • All elements after that index need to shift.

  • Therefore, time complexity is O(N).


βœ… len() β†’ O(1)

len(arr)
  • Returns the number of elements in the list.

  • Constant time operation.


Why Arrays (Lists) Are Extremely Important in DSA

Most beginner DSA problems are based on arrays:


Tuples

A tuple in Python is an ordered collection of elements, just like a list.

However, the key difference is:

πŸ‘‰ Tuples are immutable.

Once a tuple is created, its values cannot be changed.

How to Create a Tuple

t = (1, 2, 3, 4)

You can also create a tuple without parentheses:

t = 1, 2, 3

Tuple vs List (Important Difference)

Feature List Tuple
Mutable βœ… Yes ❌ No
Syntax [ ] ( )
Performance Slightly slower Slightly faster
Use Case When data may change When data should not change

Why Tuples Matter in DSA

Although tuples are not used as frequently as lists in basic DSA problems, they are very useful in certain scenarios:

1️⃣ Returning Multiple Values from a Function

def min_max(arr):
    return (min(arr), max(arr))

2️⃣ Storing Fixed Data

For example:

  • Coordinates β†’ (x, y)

  • Graph edges β†’ (node1, node2)

  • Key-value pairs


Important Interview Insight ⚑

πŸ”Ή Tuples are Hashable (if elements are immutable)

This means they can be used as keys in dictionaries or stored inside sets.

Example:

visited = set()
visited.add((1, 2))

This is very common in:

  • Graph problems

  • Grid-based problems

  • Backtracking problems

Lists cannot be used as dictionary keys because they are mutable.


Time Complexity

  • Index access β†’ O(1)

  • Searching β†’ O(N)

  • No append, insert, or remove (because immutable)


When Should You Use a Tuple?

Use a tuple when:

  • Data should not change

  • You want slightly better performance

  • You need to use the value as a dictionary key

  • You are returning multiple values from a function


Key Takeaway

Tuples are like "fixed lists."
They protect your data from accidental modification and are especially useful in hashing-related problems in DSA.