Data Structures in Python

By Seokhyeon Byun 5 min read

Note: This post is based on my old programming study notes when I taught myself.

Lists

Python doesn’t have array like C/C++ but it has lists, array module, and NumPy arrays.

  • Dynamic: Python list can be automatically resized when an element is added or removed.
  • Mutable: Element can be changed.
  • Heterogeneous: A single Python list can hold elements of different types at the same time.
arr = [1, "hello", 3.14, [2, 3], {"a": 1}]

Lists Methods

  • insert(): Add value at desired position
  • append(): Add element to list
  • pop(): Remove value at desired position (or last element)
  • clear(): Remove all values
  • sort(): Sort values in order
  • reverse(): Reverse order
  • copy(): Copy list
  • count(): Count how many times a value appears
  • index(): Find position of a value
  • len(): Count number of elements in list

Array module

Syntax is array.array(typecode, iterable)

from array import array
arr = array('i', [1, 2, 3])  # 'i' = signed integer
  • array.array(typecode, iterable) stores elements as raw C values (compact binary representation).
  • typecode is data type of array that’s represented by single characters
  • i for signed integer, f for float, and b for signed characters. This parameter is mandatory.

NumPy Array

  • Homogeneous: All elements must be the same data type
  • Fixed Size: Cannot change size after creation
  • Performance: Much faster for numerical operations than lists

Reference: NumPy Array Objects Documentation

import numpy as np
arr = np.array([1, 2, 3, 4, 5])

Dictionary

  • {key: value}
  • Useful for storing key:value pair data
  • Keys and values can be different data types
  • JSON is a text format; in Python it is usually loaded into a dict with string keys
  • A practical pattern is reading API JSON and pulling only the fields you need, for example payload["text"].

Example)

fruits={"apple":0.99, "banana": 2.50, "blueberry":3.00}

Accessing specific key values in Dictionary

fruits["apple"]
fruits["banana"]
fruits["blueberry"]

How to add new values to Dictionary

Case1: Use assignment operator

fruits["watermelon"] = 5.00
print(fruits)  # {"apple": 0.99, "banana": 2.50, "blueberry": 3.00, "watermelon": 5.00}

Case2: update() method

fruits.update({"cherry": 4.00})
print(fruits)

Like Lists, you can use len() with Dictionaries as well.

fruits = {"banana": 2, "apple": 3, "kiwi": 4}
print(len(fruits))  # 3

Accessing keys that doesn’t exist

If you try to access a key in the dictionary that isn’t there, you’ll get a KeyError and your program will crash.

You have two options to avoid doing that. You can first check if a key exists inside a dictionary using the in keyword.

if "kiwi" in fruits:
	print(fruits["kiwi"])
else:
	print("Didn't find a kiwi!")

Or you can use the in-built get() function like this.

kiwi_price = fruits.get("kiwi")
print(kiwi_price)  # None

And if the value doesn’t exist, it’ll just return None.

Or if you prefer you specify a default value for it to return instead.

kiwi_price = fruits.get("kiwi", 0)
print(kiwi_price)  # 0

Dictionary built-in methods

fruits.clear()  # Removes all items from the dictionary
fruits.get("apple")  # Gets the value of a key
fruits.items()  # Returns a list of key-value pairs
fruits.keys()  # Returns a list of all keys
fruits.pop("apple")  # Removes an item from the dictionary
fruits.update({"apple": 3})  # Adds or updates existing elements
fruits.values()  # Returns a list of all values

Serialization

  • Turning into a format that almost everybody understands. That process is called “serialization”
  • With dictionaries, it can serialize nicely into something called JSON, which stands for Javascript Object Notation
  • Python has easy ways to convert to and from JSON data.
import json

fruit_prices = {"apple": 90}
json_fruit_prices = json.dumps(fruit_prices)
print(json_fruit_prices)

In the code above, json.dumps() converts a Python dictionary into a JSON string for storage or network transfer.


Tuples

  • Immutable sequences of elements
  • The difference is that, after a tuple is created, its elements cannot be changed. You can’t add, remove, or update elements in tuple.
  • Tuples are faster and use less memory than lists, making them more efficient for large data sets. They can be used as keys in dictionaries, while lists cannot.
my_dict = {(1,2): "value"}

Creating a tuple

my_tuple = (1, 2, 3)

Access an element of tuple

# Access an element of tuple
print(my_tuple[0])
print(my_tuple[1])
print(my_tuple[2])

Unpacking or Spreading

# Instead of this...
my_tuple = (1, 2, 3)
x = my_tuple[0]
y = my_tuple[1]
z = my_tuple[2]

 # We can do this!
x, y, z = (1, 2, 3)
print(x, y, z)

Sets

  • Unordered collections of items, and are mutable
  • Use {} but unlike Dictionary there is not key, element. Only individual element exists.
  • Sets are useful for membership testing, removing duplicates, and mathematical operations like union, intersection, difference, and symmetric difference.

Create an empty set

favourite_vegetables = set()

Set Operations

  • add() - Add single element
  • update() - Add multiple elements
  • remove() - Remove element (raises error if not found)
  • discard() - Remove element (no error if not found)
  • union() - Combine sets
  • intersection() - Common elements
  • difference() - Elements in first set but not second
  • symmetric_difference() - Elements in either set but not both
# Set operations examples
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print(set1.union(set2))           # {1, 2, 3, 4, 5, 6}
print(set1.intersection(set2))    # {3, 4}
print(set1.difference(set2))      # {1, 2}
print(set1.symmetric_difference(set2))  # {1, 2, 5, 6}