AlgoDaily

Introduction to Arrays and Array Operations

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Introduction

In this lesson, we will discuss the concept of arrays, with a focus on the following key points, 1. What are arrays, and how are they implemented? 2. Working with array operations in Python.

For the Javascript version of this lesson, please click here.

Arrays are a crucial concept in programming. It is a means to store and manage data in programs. Rather than having separate variables for multiple objects, arrays allow for the storage of a collection of data. They enable programmers to store and manipulate multiple data values during the execution of the program.

Arrays

Arrays are a data structure that can store a collection of values. Depending on how they are implemented, these arrays can have items of single or multiple data types. This makes it easier to group similar elements in a program.

A data structure is a specialized format for organizing and storing a collection of data during programming.

The initialization of arrays is different in different programming languages, but the representation of arrays is the same in all of them. The items stored in an array are known as array elements. The position of each element in the array is the index. This index starts from 0 (not 1!) and allows us to find specific elements in the array. The length of an array is the number of elements the array can store or has stored in the array.

array

Understanding this representation is very helpful especially while performing array operations that we will see in the next section. For now, let's see how arrays are implemented in Python.

Arrays in Python

Arrays in Python

In Python, arrays are more commonly known as lists. In lists, elements are separated by a comma (,). They can also store elements of different data types. This means that a Python list can store a string, int, or even another list within a single list!

Now let's consider an example. Suppose we have a list of names to store in the list. The list will be initialized by giving it a name (like variables) and specifying all elements in the list separated by a comma.

name = ["Alice", "Mona", "Bob", "Barbara"]

Suppose you also want to store the corresponding ages of people in the list. You can create a list with elements of two data types (string and int) like this.

name = ["Alice", 12, "Mona", 23, "Bob", 5, "Barbara", 19]

Multiple Choice

Which of the following would generate an error in Python?

Operations on Arrays

Operations on Arrays

Array operations manipulate the array in a program. Adding, removing, searching, or printing elements in an array are the basic operations that make working with arrays much easier and helpful.

Let's discuss these array operations in detail.

Array Operations in Python

Array Operations in Python

In the previous lesson, we discussed that string operations are defined by string methods. Similar is the case for Python lists (or arrays). Their operations are also defined using list methods.

The most basic operation of lists is to add new elements to the list. The list name follows a .append() with the element to be added in the parenthesis. The new element is always inserted at the end of the list. An example code is given below.

fruits = ["apple", "orange", "grapes"]
fruits.append("strawberry")
print(fruits) # prints ["apple", "orange", "grapes", "strawberry"]

append

Another basic operation is the removal of elements from a list. For this, two kinds of methods exist in Python. .pop() removes elements by index number, and .remove() removes by element value. The index number or the element value is specified in the parenthesis. In .pop(), if the index number is not provided, then it removes the last element in the list by default. An example usage is,

fruits = ["apple", "orange", "grapes", "strawberry"]
fruits.pop(1)
print(fruits) # prints ["apple", "grapes", "strawberry"]

fruits.remove("grapes")
print(fruits) # prints ["apple", "strawberry"]

pop-remove

Another important operation of lists is accessing their length. More than often, we require lists to traverse through all elements in the list (which we will show later in the loops tutorial!). In Python, the len() method is used where the variable name for the list is specified in the parenthesis. For example,

fruits = ["apple", "orange", "grapes"]
length = len(fruits)

print(length) # prints 3

A summary of the list methods discussed for Python is listed in the table below.

Method Usage append() Insert new element at the end of list pop() Removing an element from the array using index remove() Removing an element from the list using element value len() Length of the list

A comprehensive list of all Python list methods is available here.

Fill In

What will be the output of the following code?

cars = []
cars.append("Ford")
cars.append("BMW")
cars.append("Mustang")

cars.pop(2)

True or False

The output of the following code in Python will be 3.

numbers = [3, 2, 5, 7, 3, 6]
print(numbers[3])

Summary

Summary

In this lesson, we discussed arrays and operations that can be performed on arrays. These array operations allow us to modify and access different properties of arrays during the execution of the program. In further lessons, we will see how these list methods are used in conjunction with other programming elements to generate meaningful programs and applications.

One Pager Cheat Sheet

One Pager Cheat Sheet

  • We will discuss arrays and learn how to store and manipulate multiple data values in our program.
  • Arrays are a data structure that can store a collection of values with indexes and a specific length, making it easier to group similar elements in a program.
  • In Python, lists are used to store multiple elements of different data types, such as strings and integers.
  • Both examples of list initialization in Python, with different data types stored as elements, are valid, and neither would generate an error.
  • Various operations such as adding, removing, searching and printing, can be done on arrays to make their usage in programming more efficient.
  • In Python, list operations such as append(), pop(), remove(), and len() can be used to manipulate and access information about lists (or arrays).
  • The .append() and .pop() methods of lists are used to add elements and remove the element at index 2 respectively, resulting in the list ["Ford", "BMW"].
  • The code provided will output 7, as this is the element located at index 2 in the list numbers of elements 3, 2, 5, 7, 3, 6, due to Python's indexing starting from 0 for the first element.
  • We discussed arrays and the variety of operations that can be performed on them, which can help us to create meaningful programs and applications.
Open interactive view →