Nested Arrays

In addition to basic data types, we can also insert other arrays into arrays. These inserted arrays are called nested arrays.

Nested arrays allow us to store more complex data structures. Let's look at an example:

studyGroups = [["Jennifer", "Boris"], ["David", "Ava"]]

In this example, we have the array studyGroups, which contains two other arrays, each representing a study group.

Accessing individual elements in a nested array works similarly to a one-dimensional array. For example:

print(studyGroups[0]) # Prints: ['Jennifer', 'Boris']
print(studyGroups[0][1]) # Prints: Boris

We see that studyGroups[0] is the element at index 0 in the studyGroups array, which is: ["Jennifer", "Boris"]. And when we want a value from this array, we just add square brackets with the index of the nested array: studyGroups[0][1]. This first selects the element at index 0 of the studyGroups array, which is ["Jennifer", "Boris"], and from this element, we select the element at index 1, which is "Boris".

Instructions

Print the first teammate in the second group from the teammates array.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

8/10

Nested Arrays | Start Coder