Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to get the first given number of items from the array
An array is a data structure of a set of items with the same data type, and each element is identified by an index. In Python, we can get the first few items from an array using slicing syntax array[:n].
Arrays in Python
Python does not have its own data structure to represent an array. However, we can use the list data structure as an alternative to the arrays. Here we will use list as an array
numbers = [10, 4, 11, 76, 99] print(numbers)
[10, 4, 11, 76, 99]
Python provides some modules also which are more appropriate, and the modules are NumPy and array modules.
An integer array defined by using the array module is
import array
arr = array.array('i', [1, 2, 3, 4])
print(arr)
array('i', [1, 2, 3, 4])
A NumPy array defined by the NumPy module is
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr)
[1 2 3 4]
Slicing Syntax
The slicing is used to access the group of elements from a sequence. Following is the syntax to perform slicing
sequence_object[start : end]
Where,
Start The starting index where the slicing starts. By default, it is 0.
End The ending index where the slicing stops (excluded). Default is the length of the object.
Using List Slicing
We can use the list-slicing feature to access the first given number of items from an array ?
# creating array
numbers = [1, 2, 0, 4, 1, 2, 3, 8]
print("The original array is:", numbers)
numOfItems = 4
# Get first number of elements
result = numbers[:numOfItems]
print("The first {} elements are: {}".format(numOfItems, result))
The original array is: [1, 2, 0, 4, 1, 2, 3, 8] The first 4 elements are: [1, 2, 0, 4]
Handling Edge Cases
When requesting more elements than available, slicing returns all available elements without raising an error ?
# creating small array
numbers = [1, 2, 0]
print("The original array is:", numbers)
numOfItems = 4
# Get first number of elements (more than available)
result = numbers[:numOfItems]
print("The first {} elements are: {}".format(numOfItems, result))
The original array is: [1, 2, 0] The first 4 elements are: [1, 2, 0]
Using NumPy Array
Like lists, we can also use NumPy arrays to access the first few elements using slicing ?
import numpy as np
# creating array
numpy_array = np.array([1, 3, 5, 6, 2, 9, 8])
print("The original array is:", numpy_array)
numOfItems = 3
# get first number of elements
result = numpy_array[:numOfItems]
print("The first {} elements are: {}".format(numOfItems, result))
The original array is: [1 3 5 6 2 9 8] The first 3 elements are: [1 3 5]
Using Array Module
The array module is Python's built-in module for creating typed arrays ?
import array
# creating integer array
arr = array.array('i', [2, 1, 4, 3, 6, 5, 8, 7])
print("The original array is:", arr)
numOfItems = 3
# get first elements
result = arr[:numOfItems]
print("The first {} elements are: {}".format(numOfItems, result))
The original array is: array('i', [2, 1, 4, 3, 6, 5, 8, 7])
The first 3 elements are: array('i', [2, 1, 4])
Comparison
| Method | Memory Usage | Type Safety | Best For |
|---|---|---|---|
| List | Higher | No | Mixed data types |
| NumPy Array | Lower | Yes | Numerical computations |
| Array Module | Lower | Yes | Memory-efficient arrays |
Conclusion
Use slicing syntax array[:n] to get the first n elements from any array-like structure in Python. Choose lists for flexibility, NumPy for numerical operations, or array module for memory efficiency.
