DEV Community

Cover image for Introduction to Arrays
TheCSPandz
TheCSPandz

Posted on • Edited on

Introduction to Arrays

What are Data Structures?

Data Structures are, in simpler terms, how one can structurally store data, such as in a contiguous memory location, in any memory location, or at random locations where each value points to its next.

Non-Primitive Data Structures can be classified as

  • Linear: Array, Stack, Queue, Linked List
  • Non-Linear: Trees and Graphs

This post will cover Arrays.

Linear Data Structures

As the name suggests, Linear Data Structure is a form in which each element in the structure (except the first and last element) has a neighboring element on either side.

Arrays

An array is a non-primitive data structure in which the data is stored in contiguous memory locations or, in simpler terms, memory locations that are next to the current one. Arrays, however, can only store data of the same data type, i.e., all the values in the array can be of type int, double, char, etc.

In Python, arrays can be used by accessing the array library; lists are structures that act similarly to arrays but allow storing different data types. To create an array, you can use the following:

import array as ar
demo_array = ar.array(data_type, list_of_values)
Enter fullscreen mode Exit fullscreen mode

Some of the various options available for data_type are:

  • 'i' : integer
  • 'f': float
  • 'd' : double

What are the different types of operations I can perform on arrays? Here are some of the many available ones:

  • Append: The append function adds the argument value to the end of the array; it can be used as follows:
demo_array.append(30)
Enter fullscreen mode Exit fullscreen mode
  • Insert: The insert function allows you to insert a value to any part of the array. The function .insert(i,j) takes two parameters, i: which is the index at which you would like to add the value; j: the value you would like to add;
demo_array.insert(0,45)
Enter fullscreen mode Exit fullscreen mode
  • Remove: The function removes the first instance of the argument value from the array.
demo_array.remove(40)
Enter fullscreen mode Exit fullscreen mode
  • Reverse: The function reverses the array
demo_array.reverse()
# or you can use: demo_array[::-1]
Enter fullscreen mode Exit fullscreen mode
  • Slicing: It uses a specific part of an array for a different operation. To access a sub-section of the array you can use:
    • [i:j] which considers values from i to j-1.
    • [i:] which considers values from i to the end of the array
    • [:j] which considers values from the beginning of the array to the value at j-1
    • [i:j:k], which considers values from position i to j-1 but jumps k times, the default value of k is 1, which takes every sequential element; if k is 2, it takes every alternate element, and so on.

Keeping this in mind, here is a demonstration of the usage of the various mentioned functions

Code

import array as ar

demo_array = ar.array('i',[10,204,40,40,560,2134,40,2312])
print(f"Original Array: {', '.join(map(str, demo_array))}")
demo_array.append(30)
print(f"30 appended Array: {', '.join(map(str, demo_array))}")
demo_array.insert(0,45)
print(f"45 Inserted Array: {', '.join(map(str, demo_array))}")
demo_array.remove(40)
print(f"40 removed Array: {', '.join(map(str, demo_array))}")
demo_array.reverse()
print(f"Reversed Array: {', '.join(map(str, demo_array))}")
print(f"Sliced Array: {', '.join(map(str, demo_array[3:7]))}")
print(f"Array with two steps: {', '.join(map(str, demo_array[::2]))}")
print("Printing every element in the array")
for i in (demo_array):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output

Original Array: 10, 204, 40, 40, 560, 2134, 40, 2312
30 appended Array: 10, 204, 40, 40, 560, 2134, 40, 2312, 30
45 Inserted Array: 45, 10, 204, 40, 40, 560, 2134, 40, 2312, 30
40 removed Array: 45, 10, 204, 40, 560, 2134, 40, 2312, 30
Reversed Array: 30, 2312, 40, 2134, 560, 40, 204, 10, 45
Sliced Array: 2134, 560, 40, 204
Array with two steps: 30, 40, 560, 204, 45
Printing every element in the array
30
2312
40
2134
560
40
204
10
45
Enter fullscreen mode Exit fullscreen mode

Note

When you print your array variable, as it's an object, the entire object gets printed due to the default __str__ of the module; you can either create a custom class with a custom __str__ or you can use a modified print statement as shown below:

print(f" {', '.join(map(str, demo_array))} ")
Enter fullscreen mode Exit fullscreen mode

If you would like me to integrate other languages moving forward, let me know, and I will try my best to do so. I would appreciate any suggestions related to how I can improve my content presentation.

Sources

Data Structures and Algorithms Series

Top comments (0)