Reading arrays in Python is an important skill for anybody who wants to start their journey in the wonderful world of programming with Python.
Arrays are basic data structures that hold multiple data items of the same type and are widely used to store, access, and manage data in any programming task.
Python, although doesn’t have a built-in ‘array’ data type, uses the list and other similar data types to perform similar functions as arrays in other languages. Here in this tutorial, we will learn about how to read an array in Python.
Step 1: Creation of array in Python
In Python, we generally use ‘list’ and ‘NumPy’ packages to create and handle array-like structures. A simple example of creating and printing a list is shown below. For using NumPy arrays, you may need to install the NumPy package using pip if you haven’t installed it already. You can install it using the command ‘pip install numpy’ in the terminal.
1 2 3 4 5 6 7 8 |
# Creation of list my_list = ['apple', 'banana', 'cherry'] print(my_list) # Creation of NumPy array import numpy as np my_array = np.array([1, 2, 3, 4, 5]) print(my_array) |
Step 2: Reading elements from an array
We can access elements in an array directly if we know their index. Python uses zero-based indexing, so the first element is at index 0.
1 2 3 4 5 |
# Accessing first element from list print(my_list[0]) # Accessing first element from NumPy array print(my_array[0]) |
Step 3: Reading array using list comprehension
List comprehension provides us with a concise way to create lists based on existing lists (or arrays generated from the NumPy package). It can also be used to access and perform operations on each element of a list.
1 2 3 4 5 |
# List comprehension on list print([i for i in my_list]) # List comprehension on Numpy array print([i for i in my_array]) |
Step 4: Reading array using loop
In Python, iterative reading of an array can be performed by using a ‘for’ loop which accesses each element of an array systematically starting from the first to the last element of the array.
1 2 3 4 5 6 7 |
# Looping over list for i in my_list: print(i) # Looping over Numpy array for i in my_array: print(i) |
Step 5: Reading array using an iterator
An iterator is an object that enables us to iterate over the elements of an array.
1 2 3 4 5 |
# Creating an iterator object from list: iter_obj = iter(my_list) # Access and print elements from the list print(next(iter_obj)) |
Here is the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# Creation of list my_list = ['apple', 'banana', 'cherry'] print(my_list) # Creation of NumPy array import numpy as np my_array = np.array([1, 2, 3, 4, 5]) print(my_array) # Accessing first element from list print(my_list[0]) # Accessing first element from NumPy array print(my_array[0]) # List comprehension on list print([i for i in my_list]) # List comprehension on Numpy array print([i for i in my_array]) # Looping over list for i in my_list: print(i) # Looping over Numpy array for i in my_array: print(i) # Creating an iterator object from list: iter_obj = iter(my_list) # Access and print elements from the list: print(next(iter_obj)) |
Conclusion
By following the steps outlined in this tutorial, you should now be able to read arrays in Python proficiently. Keep practicing these steps with different scenarios and arrays of your own to improve your understanding and become more comfortable with reading arrays in Python. Remember, practice is the key when it comes to mastering any programming language. Happy coding!