Python Built-in Data Structures

Introduction

Python provides several built-in data structures that allow you to store and organize data efficiently. These data structures include lists, tuples, sets, and dictionaries. Each of these data structures has its own unique properties and use cases.

Lists

A list is an ordered collection of items that can be of different types. Lists are mutable, meaning that their elements can be changed after the list is created.

Syntax

my_list = [1, 2, 3, "hello", 4.5]

Example

# Creating a list
fruits = ["apple", "banana", "cherry"]

# Accessing elements
print(fruits[0])  # Output: apple

# Modifying elements
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

# Adding elements
fruits.append("date")
print(fruits)  # Output: ['apple', 'blueberry', 'cherry', 'date']

# Removing elements
fruits.remove("blueberry")
print(fruits)  # Output: ['apple', 'cherry', 'date']

Tuples

A tuple is an ordered collection of items that can be of different types. Tuples are immutable, meaning that their elements cannot be changed after the tuple is created.

Syntax

my_tuple = (1, 2, 3, "hello", 4.5)

Example

# Creating a tuple
colors = ("red", "green", "blue")

# Accessing elements
print(colors[1])  # Output: green

# Tuples are immutable, so you cannot modify their elements
# colors[1] = "yellow"  # This will raise an error

# Tuples can be used to store related data
person = ("John", 25, "Engineer")
print(person)  # Output: ('John', 25, 'Engineer')

Sets

A set is an unordered collection of unique items. Sets are mutable, but they do not allow duplicate elements.

Syntax

my_set = {1, 2, 3, 4, 5}

Example

# Creating a set
animals = {"cat", "dog", "bird"}

# Adding elements
animals.add("fish")
print(animals)  # Output: {'cat', 'dog', 'bird', 'fish'}

# Removing elements
animals.remove("dog")
print(animals)  # Output: {'cat', 'bird', 'fish'}

# Sets do not allow duplicates
animals.add("cat")
print(animals)  # Output: {'cat', 'bird', 'fish'}

Dictionaries

A dictionary is an unordered collection of key-value pairs. Dictionaries are mutable and allow for fast lookup of values based on their keys.

Syntax

my_dict = {"key1": "value1", "key2": "value2"}

Example

# Creating a dictionary
student = {"name": "Alice", "age": 20, "major": "Computer Science"}

# Accessing values
print(student["name"])  # Output: Alice

# Modifying values
student["age"] = 21
print(student)  # Output: {'name': 'Alice', 'age': 21, 'major': 'Computer Science'}

# Adding key-value pairs
student["graduation_year"] = 2022
print(student)  # Output: {'name': 'Alice', 'age': 21, 'major': 'Computer Science', 'graduation_year': 2022}

# Removing key-value pairs
del student["major"]
print(student)  # Output: {'name': 'Alice', 'age': 21, 'graduation_year': 2022}

Summary

Python’s built-in data structures provide powerful and flexible tools for storing and organizing data:

  • Lists: Ordered, mutable collections of items. Use when you need a sequence of elements that can be changed.
  • Tuples: Ordered, immutable collections of items. Use when you need a sequence of elements that should not be changed.
  • Sets: Unordered collections of unique items. Use when you need to store non-duplicate elements and perform fast membership tests.
  • Dictionaries: Unordered collections of key-value pairs. Use when you need to associate keys with values for fast lookups.

Understanding these data structures and their use cases is essential for effective programming in Python.

Leave a Comment

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

Scroll to Top