stock market

Data Structures: How They Work & Optimize Memory

A data structures enable rapid access to stored information and seamless updates without performance bottlenecks. Whether handling large datasets or optimizing algorithms, the choice of data structure plays a crucial role in reducing processing time and memory overhead.

Purpose of Data Structures in Python

  • Efficient Storage: data structures help manage memory usage effectively, it means they optimize how data is stored, retrieved, and manipulated in a way that minimizes wasted space and maximizes efficiency.
  • Faster Data Access: Quick retrieval and updates refer to how efficiently data structures allow access to stored information and modify it without unnecessary delays.
  • Supports Algorithms: Many Python functions rely on structured data handling.

Use Cases in Python

  1. Lists (list) – Used for dynamic arrays in applications like social media posts, task lists, or data processing.
  2. Tuples (tuple) – Ideal for fixed data storage, such as coordinates or settings that shouldn’t change.
  3. Dictionaries (dict) – Powerful key-value storage, often used for databases, configurations, or caching.

1 List (list)

list in Python is a mutable , ordered collection that can store element of different data types, lists are commonly used for storing and manipulating Sequence of items.

  • Numbers
  • Strings
  • Other lists
  • Or a mix of any data types
1. How to create List

you can create a list using square brackets[]

List can Hold different data types.

2. How to Access element from list

Accessing list element using indexing , index start from 0.

3. What is slicing

Slicing is a powerful feature in Python that allows you to extract a portion (or sublist) of a list using a specific syntax. It’s useful when you want to access a range of items in a list.

Syntax of List Slicing:

  • start: Index where the slice starts (inclusive).
  • stop: Index where the slice ends (exclusive).
  • step: Interval between elements (optional).
4. Modifying Lists

lists re mutable , meaning elements can be changed

Adding elements

using append() method adds element at the end .

Removing elements

using remove() method removes the first occurrence.

Key Features of Python Lists

FeatureDescription
OrderedItems have a defined order and position (index starts from 0)
ChangeableYou can add, remove, or update items
Allows DuplicatesLists can contain duplicate values
HeterogeneousA list can store different data types (e.g., string, int, float, etc.)

Example

2.Tuple (tuple)

tuple used to store multiple items in a single variable, just like a list — but it is immutable.

How to create tuples

Tuples are created using parentheses () , and tuples can contain different data types .

How to access tuple

To access elements inside a tuple, you use indexing, which refers to the position of items starting from 0.

What is slicing in tuples

Tuple slicing is a powerful way to extract a portion of a tuple without changing the original. It works by specifying a range of indices in the format.

  • start is the index where the slice begins (inclusive).
  • stop is where it ends (exclusive).
  • Python will include items starting at start, up to but not including stop.

Example.

Key Features of Python Tuples

FeatureDescription
OrderedItems have a defined order (index starts from 0)
ImmutableYou cannot change, add, or remove items after the tuple is created
Allows DuplicatesTuples can contain duplicate values
HeterogeneousCan hold multiple data types: string, int, float, etc.
FasterSlightly faster than lists (due to immutability)

Why Use Tuples?

  • For fixed data sets like coordinates, months, or days of the week
  • Tuples can be used as dictionary keys (lists cannot)

Example

3. Dictionary (dict)

A dictionary in python is an unordered ,mutable collection of key-value pairs .dictionary in Python is a built-in data type used to store key-value pairs, each key is unique .

  • Each item in a dictionary has:
    • key (like a name or label)
    • value (the data linked to that key)

How to create Dictionary

dictionary re defined using curly braces {} with key value pairs .

Stores Key-Value Pairs

  • Think of a dictionary like a real-world dictionary:
    • The word is the key
    • The definition is the value
    • Python dictionaries use a technique called hashing to store keys.
    • Keys must be immutable (like strings, numbers, or tuples) and unique.
  • You use the key to access the value:

Dynamic Size — Memory Varies with Entries

  • The memory usage of a dictionary depends on:
    • The number of key-value pairs
    • The size of each key and value

Common Dictionary Methods

How to access dictionary

we can access dictionary using keys or using get() method .

Leave a Reply

Your email address will not be published. Required fields are marked *

RkdigitalSchool