Characteristics of array and dictionary data structures

Programming for a system using a programming language.

An array is a collection of elements, each identified by at least one array index or key. An array is a data structure that stores a fixed-size sequential collection of elements of the same type. The simplest type of array is a linear array or one-dimensional array, where elements are stored in a contiguous block of memory and can be accessed using their index, with the first element at index zero.

Arrays are useful when you need to store and manipulate a fixed number of related items, such as a list of student grades or a collection of values like temperature readings over time.

In Python, the closest equivalent to an array in other languages is ‘list’. A Python list can be used as a one-dimensional array, and lists of lists can be used to create two-dimensional arrays.

One-dimensional Arrays (Lists)

One-dimensional arrays, or simply lists in Python, are ordered sequences of elements. Each element can be accessed by its index.

# Creating a one-dimensional array (list)
one_d_array = [1, 2, 3, 4, 5]

# Accessing elements
first_element = one_d_array[0]  # Access first element (1)
last_element = one_d_array[-1]  # Access last element (5)

# Length of the array
length = len(one_d_array)  # Returns 5

Two-dimensional Arrays (Lists of Lists)

Two-dimensional arrays in Python are created by having a list where each element is itself a list.

# Creating a two-dimensional array (list of lists)
two_d_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Accessing elements
first_row = two_d_array[0] # Returns [1, 2, 3]
first_element_first_row = two_d_array[0][0] # Returns 1

# Nested iteration to traverse the 2D array
for row in two_d_array:
for element in row:
print(element, end=' ')
print() # Outputs: 1 2 3, then 4 5 6, then 7 8 9

Dictionaries

Dictionaries in Python are unordered collections of key-value pairs. Each key-value pair maps the key to its associated value. Dictionaries are mutable, which means they can be modified. They are indexed by keys, which can be any immutable type; strings and numbers always can be used as keys.

# Creating a dictionary
my_dict = {'name': 'Alice', 'age': 25, 'location': 'Wonderland'}

# Accessing elements
name = my_dict['name'] # Returns 'Alice'
age = my_dict.get('age') # Returns 25

# Adding a new key-value pair
my_dict['email'] = 'alice@example.com'

# Iterating over dictionary (keys, values, or items)
for key in my_dict:
print(key, my_dict[key]) # Outputs all key-value pairs

# Check if a key exists
if 'location' in my_dict:
print("Location is present")

# Removing a key-value pair
del my_dict['location']

Dictionaries are useful when you need to associate a set of values (the keys) with another set of values (the values) and when the keys are unique within the collection. They allow for fast retrieval, addition, and deletion of key-value pairs.

Examples where you would use a dictionary

Storing user information where you have a unique identifier for each user (such as a username) and various attributes like email address, name, and preferences.

users = {
"john_doe": {"email": "johndoe@example.com", "name": "John Doe", "age": 30},
"jane_smith": {"email": "janesmith@example.com", "name": "Jane Smith", "age": 25},
}

Word Frequency Counter: When counting the number of times each word appears in a body of text, you could use a dictionary to map each word to its frequency count.

word_counts = {
"the": 27,
"and": 15,
"to": 19,
# ...
}

Examples where you would use a one-dimensional array

Ordered Data: When you have a simple list of items that do not require key-value associations and the order is important, like a list of test scores.

test_scores = [88, 92, 79, 93, 85]

Fixed Collections: If you’re dealing with a fixed set of elements, such as the days of the week, and want to iterate over them sequentially.

days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]