An Introduction to Array in Python

Array in Python: Introduction and Functions

Operations on Array :

1. array(data type, value list) :- This function is used to create an array with data type and value list specified in its arguments. Some of the data types are mentioned in the table below.

Type Code C Type Python Type Minimum size in Bytes
‘b’ signed char int 1
‘B’ unsigned char int 1
‘u’ Py_UNICODE unicode character 2
‘h’ signed short int 2
‘H’ unsigned short int 2
‘i’ signed int int 2
‘I’ unsigned int int 2
‘l’ signed long int 4
‘L’ unsigned long int 4
‘q’ signed long long int 8
‘Q’ unsigned long long int 8
‘f’ float float 4
‘d’ double float 8

2. append() :- This function is used to add the value mentioned in its arguments at the end of the array.

3. insert(i,x) :- This function is used to add the value at the position specified in its argument.

# Python code to demonstrate the working of 
# array(), append(), insert() 

# importing "array" for array operations 
import array 

# initializing array with array values 
# initializes array with signed integers 
arr = array.array('i', [1, 2, 3]) 

# printing original array 
print ("The new created array is : ",end=" ") 
for i in range (0, 3): 
	print (arr[i], end=" ") 

print("\r") 

# using append() to insert new value at end 
arr.append(4); 

# printing appended array 
print("The appended array is : ", end="") 
for i in range (0, 4): 
	print (arr[i], end=" ") 
	
# using insert() to insert value at specific position 
# inserts 5 at 2nd position 
arr.insert(2, 5) 

print("\r") 

# printing array after insertion 
print ("The array after insertion is : ", end="") 
for i in range (0, 5): 
	print (arr[i], end=" ") 

Output:

The new created array is : 1 2 3 
The appended array is : 1 2 3 4 
The array after insertion is : 1 2 5 3 4 

4. pop() :- This function removes the element at the position mentioned in its argument, and returns it.

5. remove() :- This function is used to remove the first occurrence of the value mentioned in its arguments.

# Python code to demonstrate the working of 
# pop() and remove() 

# importing "array" for array operations 
import array 

# initializing array with array values 
# initializes array with signed integers 
arr= array.array('i',[1, 2, 3, 1, 5]) 

# printing original array 
print ("The new created array is : ",end="") 
for i in range (0,5): 
	print (arr[i],end=" ") 

print ("\r") 

# using pop() to remove element at 2nd position 
print ("The popped element is : ",end="") 
print (arr.pop(2)); 

# printing array after popping 
print ("The array after popping is : ",end="") 
for i in range (0,4): 
	print (arr[i],end=" ") 

print("\r") 

# using remove() to remove 1st occurrence of 1 
arr.remove(1) 

# printing array after removing 
print ("The array after removing is : ",end="") 
for i in range (0,3): 
	print (arr[i],end=" ") 

Output:

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 

6. index() :- This function returns the index of the first occurrence of value mentioned in arguments.

7. reverse() :- This function reverses the array.

# Python code to demonstrate the working of 
# index() and reverse() 

# importing "array" for array operations 
import array 

# initializing array with array values 
# initializes array with signed integers 
arr= array.array('i',[1, 2, 3, 1, 2, 5]) 

# printing original array 
print ("The new created array is : ",end="") 
for i in range (0,6): 
	print (arr[i],end=" ") 

print ("\r") 

# using index() to print index of 1st occurrenece of 2 
print ("The index of 1st occurrence of 2 is : ",end="") 
print (arr.index(2)) 

#using reverse() to reverse the array 
arr.reverse() 

# printing array after reversing 
print ("The array after reversing is : ",end="") 
for i in range (0,6): 
	print (arr[i],end=" ") 

Output:

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

Array in Python: Important Functions

Below are some more functions.

1. typecode :- This function returns the data type by which array is initialised.

2. itemsize :- This function returns size in bytes of a single array element.

3. buffer_info() :- Returns a tuple representing the address in which array is stored and number of elements in it.

# Python code to demonstrate the working of 
# typecode, itemsize, buffer_info() 

# importing "array" for array operations 
import array 

# initializing array with array values 
# initializes array with signed integers 
arr= array.array('i',[1, 2, 3, 1, 2, 5]) 

# using typecode to print datatype of array 
print ("The datatype of array is : ",end="") 
print (arr.typecode) 

# using itemsize to print itemsize of array 
print ("The itemsize of array is : ",end="") 
print (arr.itemsize) 

# using buffer_info() to print buffer info. of array 
print ("The buffer info. of array is : ",end="") 
print (arr.buffer_info()) 

Output:

The datatype of array is : i
The itemsize of array is : 4
The buffer info. of array is : (32497808, 6)

4. count() :- This function counts the number of occurrences of argument mentioned in array.

5. extend(arr) :- This function appends a whole array mentioned in its arguments to the specified array.

# Python code to demonstrate the working of 
# count() and extend() 

# importing "array" for array operations 
import array 

# initializing array 1 with array values 
# initializes array with signed integers 
arr1 = array.array('i',[1, 2, 3, 1, 2, 5]) 

# initializing array 2 with array values 
# initializes array with signed integers 
arr2 = array.array('i',[1, 2, 3]) 

# using count() to count occurrences of 1 in array 
print ("The occurrences of 1 in array is : ",end="") 
print (arr1.count(1)) 

# using extend() to add array 2 elements to array 1 
arr1.extend(arr2) 

print ("The modified array is : ",end="") 
for i in range (0,9): 
	print (arr1[i],end=" ") 

Output:

The occurrences of 1 in array is : 2
The modified array is : 1 2 3 1 2 5 1 2 3 

6. fromlist(list) :- This function is used to append a list mentioned in its argument to end of array.

7. tolist() :- This function is used to transform an array into a list.

# Python code to demonstrate the working of 
# fromlist() and tolist() 

# importing "array" for array operations 
import array 

# initializing array with array values 
# initializes array with signed integers 
arr = array.array('i',[1, 2, 3, 1, 2, 5]) 

# initializing list 
li = [1, 2, 3] 

# using fromlist() to append list at end of array 
arr.fromlist(li) 

# printing the modified array 
print ("The modified array is : ",end="") 
for i in range (0,9): 
	print (arr[i],end=" ") 

# using tolist() to convert array into list 
li2 = arr.tolist() 

print ("\r") 

# printing the new list 
print ("The new list created is : ",end="") 
for i in range (0,len(li2)): 
	print (li2[i],end=" ") 

Output:

The modified array is : 1 2 3 1 2 5 1 2 3 
The new list created is : 1 2 3 1 2 5 1 2 3 

#python

An Introduction to Array in Python
14.85 GEEK