Python - Arrays

Why should I learn to solve Python: Arrays technical interview questions?

Learn and practise solving Python: Arrays technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.

Where can I get technical Python: Arrays technical interview questions and answers with explanations?

IndiaBIX provides you with lots of fully solved Python: Arrays technical interview questions and answers with a short answer description. You can download Python: Arrays technical interview questions and answers as PDF files or e-books.

How do I answer Python: Arrays technical interview questions from various companies?

You can answer all kinds of Python: Arrays technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Arrays technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.

1.
What is an array in Python?

In Python, an array is not a built-in data type like lists or dictionaries. Instead, it is typically represented using the array module or the more commonly used numpy library. However, it's important to note that Python lists are often used as a flexible alternative to traditional arrays in other programming languages.

Example using the array module:

import array

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

# Accessing elements in the array
print("Array elements:", my_array[0], my_array[1], my_array[2])

# Adding elements to the array
my_array.append(6)
print("Updated array:", my_array)

Output:

Array elements: 1 2 3
Updated array: array('i', [1, 2, 3, 4, 5, 6])

Example using the numpy library:

import numpy as np

# Creating an array using numpy
my_array = np.array([1, 2, 3, 4, 5])

# Performing operations on the array
result = my_array * 2
print("Result after multiplication:", result)

Output:

Result after multiplication: [ 2  4  6  8 10]

While Python lists are more versatile, arrays provided by the array module or numpy are more efficient for numerical operations.


2.
How do you create an empty array in Python?

Creating an empty array in Python can be done using either the array module or the numpy library. Below are examples for both approaches:

Using the array module:

import array

# Creating an empty array of integers
empty_array = array.array('i')

# Displaying the empty array
print("Empty Array:", empty_array)

Output:

Empty Array: array('i')

Using the numpy library:

import numpy as np

# Creating an empty array using numpy
empty_array = np.array([])

# Displaying the empty array
print("Empty Array:", empty_array)

Output:

Empty Array: []

Both approaches create an empty array that can be later populated with elements.


3.
Explain the difference between a list and an array in Python.

In Python, a list and an array have some differences in terms of functionality and usage.

A list is a built-in data structure in Python that can hold elements of different data types. It is versatile and can be easily modified. Lists are part of the standard Python language, and no additional module is required to use them.

# Example of a list
my_list = [1, 2, 3, 'hello', 4.5]

# Displaying the list
print("List:", my_list)

Output:

List: [1, 2, 3, 'hello', 4.5]

An array, on the other hand, is a part of the array module or the numpy library. It is a more specialized data structure that can only hold elements of the same data type. Arrays are generally used for numerical computations and operations.

Using the array module:

import array

# Example of an array
my_array = array.array('i', [1, 2, 3, 4, 5])

# Displaying the array
print("Array:", my_array)

Output:

Array: array('i', [1, 2, 3, 4, 5])

Using numpy:

import numpy as np

# Example of a numpy array
my_np_array = np.array([1, 2, 3, 4, 5])

# Displaying the numpy array
print("NumPy Array:", my_np_array)

Output:

NumPy Array: [1 2 3 4 5]

In summary, lists are more general-purpose and flexible, while arrays are specialized for numerical computations and have a more rigid structure.


4.
What is the purpose of the array module in Python?

The array module in Python provides a way to create arrays, which are a more memory-efficient alternative to lists, especially when dealing with large datasets of the same data type. The array module defines a sequence data type called array, which is similar to a list but stores data more compactly in a fixed-size array.

Here is an example of using the array module to create an array of integers:

import array

# Create an array of integers ('i' type code)
my_array = array.array('i', [1, 2, 3, 4, 5])

# Display the array
print("Array:", my_array)

Output:

Array: array('i', [1, 2, 3, 4, 5])

In this example, array('i', [1, 2, 3, 4, 5]) creates an array of integers with type code 'i' and initializes it with the given list of values.

The array module provides an efficient way to store and manipulate homogeneous data, such as a large set of numerical values. It is particularly useful in situations where memory optimization is crucial.