Open In App

Array in Python | Set 2 (Important Functions)

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Array in Python | Set 1 (Introduction and Functions)

Array in Python | Set 2

Below are some more useful functions provided in Python for arrays:

Array Typecode Function

This function returns the data type by which the array is initialized. In this example, we are using arr.typecode to find out the data type of array initialization.

Python3




# importing "array" for array operations
import array
    
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
   
# using typecode to print datatype of array
print ("The datatype of array is : ")
print (arr.typecode)


Output

The datatype of array is : 
i

Array itemsize Function

This function returns the size in bytes of a single array element. In this example, we are using itemsize function to find out the size in byte of an array element.

Python3




# importing "array" for array operations
import array
    
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
   
# using itemsize to print itemsize of array
print ("The itemsize of array is : ")
print (arr.itemsize)


Output

The itemsize of array is : 
4

buffer_info() in Python

Returns a tuple representing the address in which array is stored and number of elements in it. In this example, we are using buffer_info() to do the same.

Python3




# importing "array" for array operations
import array
    
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
 
# using buffer_info() to print buffer info. of array
print ("The buffer info. of array is : ")
print (arr.buffer_info())


Output

The buffer info. of array is : 
(140491260368688, 6)

count() in Python

Python count() function counts the number of occurrences of argument mentioned in array.

extend() in Python

This function appends a whole array mentioned in its arguments to the specified array. In this example, we are using extend() to append another array.

Python3




# importing "array" for array operations
import array
    
# initializing array with array values
arr1 = array.array('i',[1, 2, 3, 1, 2, 5])
arr2 = array.array('i',[1, 2, 3])
 
# using extend() to add array 2 elements to array 1
arr1.extend(arr2)
   
print ("The modified array is : ")
for i in range (0,9):
    print (arr1[i], end=" ")


Output

The modified array is : 
1 2 3 1 2 5 1 2 3 

Array fromlist() Function

This function is used to append a list mentioned in its argument to end of array. In this example, we are using fromlist() to append a list to end of array.

Python3




# importing "array" for array operations
import array
    
# initializing array with array values
arr = array.array('i',[1, 2, 3, 1, 2, 5])
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=" ")


Output

The modified array is : 1 2 3 1 2 5 1 2 3 

tolist() in Python

This function is used to transform an array into a list. In this example, we are using tolist() to convert an array to list.

Python3




# importing "array" for array operations
import array
    
# initializing array with array values
arr = array.array('i',[1, 2, 3, 1, 2, 5])
   
# using tolist() to convert array into list
li2 = arr.tolist()
   
# printing the new list
print ("The new list created is : ",end="")
for i in range (0,len(li2)):
    print (li2[i],end=" ")


Output

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


Similar Reads

Complex Numbers in Python | Set 2 (Important Functions and Constants)
Introduction to python complex numbers: Complex Numbers in Python | Set 1 (Introduction) Some more important functions and constants are discussed in this article. Operations on complex numbers : 1. exp() :- This function returns the exponent of the complex number mentioned in its argument. 2. log(x,b) :- This function returns the logarithmic value
4 min read
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Array in Python | Set 1 (Introduction and Functions)
Other than some generic containers like lists, Python in its definition can also handle containers with specified data types. The array can be handled in Python by a module named "array". They can be useful when we have to manipulate only specific data type values. Properties of ArraysEach array element is of the same data type and size. For exampl
7 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
View Computer's Important Network Information Using Python
While working in a network, we do some troubleshooting with the network or Internet problem. This time we need to check your own system network connection information. We can find the network connection in the Control Panel in Windows. The best way to find this information we can use ipconfig command in CMD. When you use ipconfig /all then you can
1 min read
Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
Python has defined a module, "time" which allows us to handle various operations regarding time, its conversions and representations, which find its use in various applications in life. The beginning of time started measuring from 1 January, 12:00 am, 1970 and this very time is termed as "epoch" in Python. Operations on Time in Python Python time.t
4 min read
Python | Set 2 (Variables, Expressions, Conditions and Functions)
Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on the system you are working on. If it is not installe
3 min read
Article Tags :
Practice Tags :