Reverse of Array in Python
Python Arrays:
A group of objects that are kept in consecutive memory locations is known as an array. It is a compartment with a predefined number of identical items that can fit inside it. Arrays are widely used in most computer languages, such as C/C++, JavaScript, etc.
The ability to group components of the same form in an array makes it simpler to locate specific elements by simply appending an attenuation to the base value. By reducing the code size, combining the arrays might accelerate computation. It is used to store values in one variable. If your list of items is organised into the corresponding variables as follows:
A data structure called an array in Python stores a group of objects. Any data type, including objects, strings, and numbers, is acceptable as an item type in an array. An array data structure is available thanks to Python's built-in array module. The array class is employed to create arrays. It requires two arguments: the type of data of the components that will be saved in the array and an optional initialiser again for array elements. The following code, for instance, can be used to start creating an array of integers
Example:
import array as arr
a = arr.array('i', [5, 3, 1, 9])
print("1st Element:", a[0])
print("2nd element:", a[1])
print("last element:", a[-1])
OUTPUT:
1st element: 5
2nd element: 3
last element: 9
CODE EXPLANATION:
This code imports the array module and defines an integer array using the array() constructor from the array module. The first argument to the array() constructor, 'i', specifies the type code for the elements of the array; in this case, 'i' represents an integer. The second argument is a list of integers [5, 3, 1, 9], which are the elements of the array. The code then uses the print() function to display array a's first, second, and last elements. The first element of the array a is accessed using index 0, the second is accessed using index 1, and the last is accessed using index -1.
An Array is a module in Python that handles arrays. It is useful when we want to manipulate only specific data values. The definitions provided below will help you comprehend the idea of an array.
Element: An element is any item maintained in an array.
Index: A component in an array is located using a mathematical index that specifies
its location.
Representation of Array:
Using each element's index, we can access it. The index begins at zero. The array's length determines how many elements can be saved there.
Array Operations:
Traverse: It prints each element individually.
Insertion: This method adds an element to the specified index.
Deletion: The component at the designated index is eliminated.
Search: It searches an element by its value or the specified index.
Update: By using this function, the component at the given index is updated.
Reversing an Array Elements using For loop:
arr = [1, 2, 3, 4, 5];
print("Original array: ");
l=[]
for i in range(0, len(arr)):
l.append(i)
print(l)
r=[]
print("Array in reverse order: ");
#Reversing the order of the array's iterations
for i in range(len(arr)-1, -1, -1):
r.append(i)
print(r)
OUTPUT:
Original array:
[0, 1, 2, 3, 4]
Array in reverse order:
[4, 3, 2, 1, 0]
CODE EXPLANATION:
This code defines an array arr with the elements [1, 2, 3, 4, 5]. The code then creates two new empty arrays, l and r.
The first loop, using for i in range(0, len(arr)), iterates through the elements of arr in the order they are defined. For each iteration, the current value of i is appended to the list l. After all iterations, l will contain the values [0, 1, 2, 3, 4].
The second loop, using for i in range(len(arr)-1, -1, -1), iterates through the elements of arr in reverse order. For each iteration, the current value of i is appended to the list r. After all iterations, r will contain the values [4, 3, 2, 1, 0]. It is important to note that while the arrays l and r are related to the original array arr, they are not copies of arr and do not contain the same elements.
Using a Built-In Function:
numbers = list(map(int,input("Enter elements:").split()))
print("Original Array:")
print(numbers)
numbers.reverse()
print("Array after Reversing:")
print(numbers)
OUTPUT:
Enter elements:1 56 2 7 8
Original Array:
[1, 56, 2, 7, 8]
Array after Reversing:
[8, 7, 2, 56, 1]