Cover Image for Python Mutable Data Types
237 views

Python Mutable Data Types

The Python mutable data types are data structures whose contents or values can be changed after they are created. Mutable data types allow you to modify, add, or remove elements or values without creating a new object. Here are some commonly used mutable data types in Python:

Lists (list):

  • Lists are ordered collections of elements.
  • You can modify elements, append or remove elements, and reorder the elements within a list.
  • Example:
Python
# Creating a list with elements 1, 2, and 3
my_list = [1, 2, 3]

# Modifying the element at index 1 to have the value 4
my_list[1] = 4

# Appending the element 5 to the end of the list
my_list.append(5)

# Removing the element 3 from the list
my_list.remove(3)

Dictionaries (dict):

  • Dictionaries are unordered collections of key-value pairs.
  • You can modify values associated with keys, add new key-value pairs, or remove key-value pairs.
  • Example:
Python
# Creating a dictionary with keys 'name' and 'age'
my_dict = {'name': 'Alice', 'age': 30}

# Modifying the value associated with the key 'age' to 31
my_dict['age'] = 31

# Adding a new key 'city' with the value 'New York'
my_dict['city'] = 'New York'

# Deleting the key 'age' and its associated value from the dictionary
del my_dict['age']

Sets (set):

  • Sets are unordered collections of unique elements.
  • You can add, remove, or update elements in a set.
  • Example:
Python
# Creating a set with elements 1, 2, and 3
my_set = {1, 2, 3}

# Adding the element 4 to the set
my_set.add(4)

# Removing the element 2 from the set
my_set.remove(2)

Byte Arrays (bytearray):

  • Byte arrays are similar to bytes but mutable. They allow you to modify individual elements.
  • Example:
Python
# Creating a bytearray from the bytes literal b'Hello'
my_bytes = bytearray(b'Hello')

# Modifying the first element of the bytearray to have the ASCII value of lowercase 'h'
my_bytes[0] = ord('h')

Custom Objects:

  • You can create custom classes with mutable attributes to represent mutable data types.
  • Example:
Python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Creating an instance of the Person class
person = Person('Alice', 30)

# Modifying the age attribute of the person instance
person.age = 31

Array Module (array.array):

  • The array module provides a mutable array data type.
  • You can modify elements in an array using indexing.
  • Example:
Python
import array

# Creating an array of signed integers with initial values [1, 2, 3]
my_array = array.array('i', [1, 2, 3])

# Modifying the element at index 1 to have the value 4
my_array[1] = 4

Mutable data types are useful when you need to represent and manipulate collections of data that may change over time. However, you should be aware that their mutability can lead to unintended side effects, especially in multi-threaded or concurrent programs. You should use them with caution and consider immutability when it’s important to prevent unintended modifications.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS