Back to Data Structures and Algorithms
Data Structures and Algorithms

Data Structures and Algorithms

0 of 20 Completed

Understanding Arrays

Often times in Python, programmers are exposed to lists rather than arrays. Functionally, they are very similar, but do have important differences.

First, we will review what an array is, then cover the similarities and differences that they have with lists.

At its most basic level, arrays let us hold data, and allow us to access individual data elements using indexes.

At its most basic level, arrays let us hold data, and allow us to access individual data elements using indexes.

# lists / arrays can be declared by enclosing elements in square brackets "[]"
fruits = ['apple', 'orange', 'grapes', 'lemon']

# you can access the elements inside the array using indexes through square brackets
f = fruits[1] 
print(f) # prints out 'orange'

Indexes

In most programming languages, including Python, arrays are 0-indexed, implying that its indexes start at zero. The first element will be at index 0, the second element at index 1, and so on.

Examples

fruits = ['apple', 'orange', 'grapes', 'lemon']

# access the first element
apple = fruits[0]

⚠️ An index should only range from zero until the length of the array - 1. You can use the len function to check for the length of an array. If you try to use an index outside of this range, or if the array is empty, you will see an IndexError , which indicates that the list index you are trying to use is out of range.

Challenge Question 1: Basic Indexing

I have an array of items called items. How can I access the tenth element?

a. items[10]

b. items[11]

c. items[9]

d. items['tenth']

Array Iteration

Iteration, the process of traversing each element in an array, is a common operation. It’s especially useful for tasks like sorting, searching, or modifying array contents when the index of a specific item is unknown.

Index-Based Iteration

Sometimes, you may need to know the position of the element in the array. In such cases, you can iterate using indices.

Example:

fruits = ['apple', 'orange', 'grapes', 'lemon']

# Iterating over the array using indices
for i in range(len(fruits)):
    print("fruit number", i, "is",  fruits[i]) 
# Here, we use the brackets to access the elements

Here, range(len(fruits)) generates a sequence of indices from 0 to the length of the fruits array minus one. The variable i takes each of these indices, allowing you to access and print both the index and the corresponding element.

Index-based iteration is helpful when we need to modify the placements of elements (i.e., sorting) or when we need to know the index for later use (i.e., searching).

Index Based Iteration

For Loop Iteration

In Python, the for loop provides a simple and efficient way to iterate over arrays (lists). It sequentially processes each element in the array, eliminating the need to manage the index manually. This contrasts with the typical behavior of for loops in many other programming languages, where they are more akin to index-based iteration.

In Python’s for loop, you iterate directly over the elements of a collection, such as a list. During each iteration, a variable is assigned the current element from the array. This style of loop, where a variable takes on the value of each element in a collection, is known in some other programming languages as a “for-each” or “range-based” for loop.

Example:

fruits = ['apple', 'orange', 'grapes', 'lemon']

# Using Python's for loop to iterate over an array
for fruit in fruits:
    print(fruit)

In this example, fruit is the variable that takes on the value of each element in the fruits array as the loop progresses. It starts with ‘apple’, then ‘orange’, and so on, until all elements in the array have been processed.

Enumerate

An alternative to index-based iteration is Python’s enumerate function. In a normal for loop, the for statement will return the current pointed element. However, using enumerate, it will also return the counter (starting at zero), indicating that the current element is the n+1th element.

Example:

fruits = ['apple', 'orange', 'grapes', 'lemon']

# Iterating with enumerate to get index and value
for index, fruit in enumerate(fruits):
    print("fruit number", index, "is",  fruit) 

In this code, enumerate(fruits) returns a tuple containing the index and the value of each element. This allows you to access and print both the index (index) and the element (fruit) directly. Enumerate inherently combines both the advantages of for-each (not requiring indexes) and index-based iteration (aware of the current index).

Additional Reading

Lists VS. Arrays

Throughout this section of the course, we have used arrays and lists interchangeably. In the context of Python, the list is Python’s built-in implementation of an array. However, there are key differences. Specifically, let’s look at statically-typed languages, such as Java, C++, and Go.

In these languages, arrays are expected to hold elements of the same type. While you can easily declare arrays containing differently typed elements in Python, such as:

array = ['string', 12, 3.14]

The same cannot be said for other programming languages. Arrays must hold elements of the same type, such as in Java:

int[] arr = {1, 2, 3, 4, 5}; // here, the array must be of type int

Moreover, in most programming languages, arrays are fixed-size. If you look at that Java array for example, there are no methods to adding elements to such an array. This contrasts to Python where arrays can be as big as possible. There are ways to make array sizes dynamic (like in Python), as to which, vectors exist. In C++, you have the std::vector and in Java, the ArrayList<T> and the Vector<T> exist.

Uses of List

Lists Vs Tuples

In Python, when working with array-like data structures, two commonly used types are lists and tuples. Let’s explore the distinctions between these two data types and understand when to choose one over the other.

Lists:

A list is a versatile and mutable data type. You can modify its content in-place, making it suitable for situations where dynamic changes to the data are required. For example:


my_list = [1, 2, 3]
my_list.append(4)  # Modifies the list in-place
print(my_list)    # Output: [1, 2, 3, 4]

Tuples:

On the other hand, a tuple is a fixed-length immutable data type. Because a tuple is immutable, all operations are not done in place. For instance, if you were to add an item to a tuple, it does not modify the tuple but instead creates a new instance of the tuple and appends the new item:

my_tuple = (1, 2, 3)
new_tuple = my_tuple + (4,)  # Creates a new tuple with the added item
print(new_tuple)             # Output: (1, 2, 3, 4)

One notable advantage of tuples is their immutability. Since tuples cannot be modified after creation, they provide a level of data integrity that can be crucial in certain scenarios. This immutability also makes tuples suitable for use as keys in dictionaries or elements in sets.

Understanding the differences between lists and tuples is essential for choosing the appropriate data structure based on the specific requirements of your program. Each has its own strengths and use cases, and making an informed decision can lead to more efficient and maintainable code.

Arrays in Memory

The main principle of an array is that it is often the fastest and most efficient data structure to use. The secret to this is how an array is stored in memory. Arrays (and yes, this includes Python lists) are stored as a contiguous block of data in memory. Because they are contiguous, navigating through the data is relatively easy. The processor can also apply optimizations to make this process faster

0%

Completed

You have 20 sections remaining on this learning path.