Home » Data Science » Python » Python Arrays

Newly Updated Posts

Python Arrays

In this tutorial, we will learn Arrays in Python Programming:

In Python, arrays can be implemented using several different data structures, depending on the specific requirements of your program. The built-in list type is the most commonly used and flexible data structure for creating arrays in Python. Lists are ordered, mutable (can be modified), and can contain elements of different data types. Here’s an example of creating and manipulating a list in Python:

# Creating a list
my_list = [1, 2, 3, 4, 5]
print(my_list)  # Output: [1, 2, 3, 4, 5]

# Accessing elements
print(my_list[0])  # Output: 1
print(my_list[2])  # Output: 3

# Modifying elements
my_list[3] = 10
print(my_list)  # Output: [1, 2, 3, 10, 5]

# Appending elements
my_list.append(6)
print(my_list)  # Output: [1, 2, 3, 10, 5, 6]

# Removing elements
my_list.remove(2)
print(my_list)  # Output: [1, 3, 10, 5, 6]

Another data structure commonly used for arrays in Python is the array module, which provides a more memory-efficient way of storing homogeneous data (elements of the same data type) compared to lists. Here’s an example:

import array

# Creating an array of integers
my_array = array.array('i', [1, 2, 3, 4, 5])
print(my_array)  # Output: array('i', [1, 2, 3, 4, 5])

# Accessing elements
print(my_array[0])  # Output: 1
print(my_array[2])  # Output: 3

# Modifying elements
my_array[3] = 10
print(my_array)  # Output: array('i', [1, 2, 3, 10, 5])

In addition to lists and arrays, there are other specialized data structures available in Python for handling arrays, such as NumPy arrays and pandas DataFrames, which offer more advanced functionality for numerical computing and data analysis tasks.

Adding Elements to a Array

To add elements to an array in Python, you can use the append() method or the + operator. Here’s how you can do it:

  1. Using append() method:
my_array = [1, 2, 3]  # Initial array
my_array.append(4)    # Add element 4 at the end
print(my_array)       # Output: [1, 2, 3, 4]

In this example, the append() method is called on the array my_array and passed the value 4 to add it to the end of the array.

2. Using the + operator:

my_array = [1, 2, 3] # Initial array 
my_array = my_array + [4] # Add element 4 at the end 
print(my_array)   # Output: [1, 2, 3, 4]

In this example, the + operator is used to concatenate the original array my_array with another list [4], resulting in a new array with the added element.

Both approaches achieve the same result of adding elements to an array. However, keep in mind that the append() method modifies the original array in-place, while using the + operator creates a new array. Choose the approach that best suits your needs based on whether you want to modify the original array or create a new one.

Accessing elements from the Array python

In Python, you can access elements from an array using their indices. The index represents the position of the element within the array, starting from 0 for the first element. Here’s how you can access elements from an array in Python:

# Define an array
my_array = [10, 20, 30, 40, 50]

# Access elements by index
print(my_array[0])  # Output: 10 (first element)
print(my_array[2])  # Output: 30 (third element)
print(my_array[-1])  # Output: 50 (last element)

# Update elements
my_array[1] = 25
print(my_array)  # Output: [10, 25, 30, 40, 50]

# Slicing arrays
print(my_array[1:4])  # Output: [25, 30, 40] (elements from index 1 to 3)
print(my_array[:3])  # Output: [10, 25, 30] (elements from start to index 2)
print(my_array[2:])  # Output: [30, 40, 50] (elements from index 2 to end)

You can access elements by specifying the index within square brackets []. Negative indices can be used to access elements from the end of the array. You can also update elements by assigning new values to the specified index. Slicing allows you to extract a subset of elements from the array by specifying a range of indices.