Python Arrays



For simplicity`s sake, we can imagine an array of stairs in which a value is placed at each step (say, one of your friends). Here, you can locate any of your friends simply by knowing the number of steps they are on. An array can be processed in Python by a module named array . They can be useful when we only need to manipulate values ​​of a specific data type. The user can treat

Create Array

Array in Python can be built by importing the array module. array ( data_type , value_list ) is used to create an array with the data type and list of values ​​specified in its arguments.

# Python program for demonstration
# Create an array

 
# import & quot; array & quot; to create an array

import array as arr

 
# create an array with an integer type

a = arr.array ( `i` , [ 1 , 2 , 3 ])

 
# print the original array

print ( "The new created array is:" , end = "" )

for i in range ( 0 , 3 ):

print (a [i], end = " " )

print ()

 
# create an array of float type

b = arr.array ( `d` , [ 2.5 , 3.2 , 3.3 ])

 
# printing the original array

print ( "The new created array is:" , end = "" )

for i in range ( 0 , 3 ):

print (b [i], end = "" )

 

Output:

 The new created array is: 1 2 3 The new created array is: 2.5 3.2 3.3 

Some of the datatypes are mentioned below to help you create an array of different datatypes.

Adding items to an array

Items can be added to an array using the built-in function append () is also used to append the value specified in its arguments to the end of the array .

# Python program for demonstration
# Adding elements to the array

 
# import & quot; array & quot; to create an array

import array as arr

 
# array of int type

a = arr.array ( `i` , [ 1 , 2 , 3 ])

 

 

print ( "Array before insertion:" , end = " " )

for i in range ( 0 , 3 ):

print (a [i], end = "" )

print ()

 
# insert array using
# insert () function

a.insert ( 1 , 4 )

 

print ( " Array after insertion: " , end = "" )

for i in (a):

print (i, end = "" )

print () 

 
# array of float type

b = arr.array ( `d` , [ 2.5 , 3.2 , 3.3 ])

  

print ( "Array before insertion:" , end = "" )

for i in range ( 0 , 3 ):

  print (b [i], end = "" )

print ()

 
# adding an element using append ()

b.append ( 4.4 )

 

print ( "Array after insertion:" , end = "" )

for i in (b):

print (i, end = "" )

print () 

Output:

 Array before insertion: 1 2 3 Array after insertion: 1 4 2 3 Array before insertion: 2.5 3.2 3.3 Array after insertion: 2.5 3.2 3.3 4.4 

Accessing elements from an array

Refer to the index number to access the elements of an array. Use the subscript operator [] to access an element in an array. The index must be an integer.

# Python program to demonstrate
# access the element from list

 
# array module import

import array as arr

 
# array of int type

a = arr.array ( `i` , [ 1 , 2 , 3 , 4 , 5 , 6 ])

  
# access to array element

print ( "Access element is:" , a [ 0 ])

 
# access to array element

print ( "Access element is:" , a [ 3 ])

 
# float array

b = arr.array ( `d` , [ 2.5 , 3.2 , 3.3 ])

 
# access to array element

print ( "Access element is:" , b [ 1 ])

  
# access to array element

print ( "Access element is:" , b [ 2 ])

Output:

 Access element is: 1 Access element is : 4 Access element is: 3.2 Access element is: 3.3 

Removing items from an array

Items can be removed from an array using the built-in function pop () function can also be used to remove and return an item from an array, but by default it only removes the last element of the array, to remove an element from a specific position in the array, the index of the element is passed as an argument to the pop () method.

Note. The Remove method in the List will only remove the first occurrence of the searched element.

# Python program for demonstration
# Removing elements in an array

 
# importing an "array" for array operations

import array

 
# initializes array with array values ​​
# initializes signed array integers

arr = array.array ( `i` , [ 1 , 2 , 3 , 1 , 5 ]) 

 
# print the original array

print ( "The new created array is: " , end = " ")

for i in range ( 0 , 5 ):

print (arr [i], end = " " )

 

print ( "" )

 
# using pop () to remove element at 2nd position

print ( "The popped element is:" , end = "")

print (arr.pop ( 2 ))

  
# print array after it appears

print ( "The array after popping is:" , end = "")

for i in range ( 0 , 4 ):

print (a rr [i], end = "" )

 

print ( "" )

 
# using remove () to remove 1st occurrence 1

arr.remove ( 1 )

 
# print array after deletion

print ( "The array after removing is:" , end = " ")

for i in range ( 0 , 3 ):

print (arr [i], end = " " )

Exit:

 The new created array is: 1 2 3 1 5 The popped element is: 3 The array after popping is: 1 2 1 5 The array after removing is: 2 1 5 

Slicing an array

In a Python array, there are several ways to print an entire array with all elements, but to print a specific range of elements from an array, we use the

# Python program for demonstration
# silting up elements in an array

 
# array module import

import array as arr

 
# create a list

l = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]

 

a = arr.array ( ` i` , l)

print ( "Intial Array:" )

for i in (a):

print (i, end = "" )

 
# Print range items
# using the Slice operation

Sliced_array = a [ 3 : 8 ]

print ( "Slicing elements in a range 3-8:" )

print (Sliced_array)

 
# Print items from
# predefined point to end

Sliced_array = a [ 5 :]

print ( "Elements sliced ​​from 5th"

"element till the end:" )

print (Sliced_array)

  
# Print items from
# start to finish

Sliced_array = a [:]

print ( "Printing all elements using slice operation:" )

print (Sliced_array)

Output:

 Intial Arr ay: 1 2 3 4 5 6 7 8 9 10 Slicing elements in a range 3-8: array (`i`, [4, 5, 6, 7, 8]) Elements sliced ​​from 5th element till the end: array ( `i`, [6, 7, 8, 9, 10]) Printing all elements using slice operation: array (` i`, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ) 

Finding an element in an array

To find an element in an array, we use the built-in Python method index () . This function returns the index of the first occurrence of the value specified in the arguments.

# Python code for demo
# find element in array

 

 
# array module import

import array

 
# array initialization with array values ​​
# initializes an array with signed integers

arr = array.array ( `i` , [ 1 , 2 , 3 , 1 , 2 , 5 ]) 

 
# print the original array

print ( "The new created array is:" , end = "")

for i in range ( 0 , 6 ):

print (arr [i], end = "" )

 

print ( "" )

 
# using index () to print the first occurrence index 2

print ( "The index of 1st occurrence of 2 is:" , end = "")

print (arr.index ( 2 ))

 
# using index () to print the index of the first occurrence 1

print ( "The index of 1st occurrence of 1 is:" , end = "")

print ( arr.index ( 1 ))

Exit:

 The new created array is: 1 2 3 1 2 5 The index of 1st occurrence of 2 is: 1 The index of 1st occurrence of 1 is: 0 

Updating items in an array

To update an item in an array, we just reassign the new value to the desired index we want to update.

# Python code for demo
# how to update an element in an array

 code class = "plain"> ( "" )

 
# using index () to print the first occurrence index 2

print ( "The index of 1st occurrence of 2 is:" , end = "")

print (arr.index ( 2 ))

 
# using index () to print the index of the first occurrence 1

prin