0% found this document useful (0 votes)
14 views3 pages

Python Data Structures Beginner Notes

Uploaded by

Mahnoor Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Python Data Structures Beginner Notes

Uploaded by

Mahnoor Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Data Structures - Beginner Notes

1. List

A dynamic array (resizable), stores elements in ordered form.

Example:

fruits = ["apple", "banana", "cherry"]

fruits.append("orange")

fruits.insert(1, "kiwi")

fruits.remove("banana")

Time Complexity:

Access: O(1), Append: O(1), Insert/Delete: O(n), Search: O(n)

2. Tuple

Immutable ordered list.

Example:

coords = (10, 20)

print(coords[0])

Time Complexity:

Access: O(1), Search: O(n)

3. Set

Unordered collection with unique items.

Example:

nums = {1, 2, 3}

nums.add(4)

nums.remove(2)

Time Complexity:

Add/Remove/Search: O(1)

4. Dictionary

Key-value store.

Example:

person = {"name": "Alice", "age": 25}


person["job"] = "Engineer"

del person["age"]

Time Complexity:

Get/Set/Delete: O(1), Iterate: O(n)

5. Queue (collections.deque)

FIFO structure.

Example:

from collections import deque

q = deque()

q.append("A")

q.popleft()

Time Complexity:

Append/Pop Left: O(1)

6. Stack (list)

LIFO structure.

Example:

stack = []

stack.append(5)

stack.pop()

Time Complexity:

Push/Pop: O(1)

7. Heap (heapq)

Min-heap for priority queue.

Example:

import heapq

nums = [5, 2, 8]

heapq.heapify(nums)

heapq.heappush(nums, 1)

heapq.heappop(nums)

Time Complexity:

heappush/heappop: O(log n), heapify: O(n)


8. Linked List

Nodes connected via pointers.

Example:

class Node:

def __init__(self, val):

self.val = val

self.next = None

class LinkedList:

def add_front(self, val):

new_node = Node(val)

new_node.next = self.head

self.head = new_node

Time Complexity:

Insert/Delete: O(1), Access/Search: O(n)

Comparison Summary

List: fast access, slow insert/delete

Tuple: immutable, fast

Set: fast lookup, no duplicates

Dict: fast key-value operations

Queue: FIFO with O(1) ops

Stack: LIFO with O(1) ops

Heap: efficient min/max

Linked List: good for dynamic memory

You might also like