Array in Python | Bundle 2 (Essential Features)



Here are some more features.

1. typecode : — this function returns the data type to which the array is initialized.

2. itemsize : — this function returns the size in bytes of one element of the array.

3. buffer_info () : — returns a tuple representing the address where the array is stored and the number of elements in it.

# Python code to demonstrate how it works
# typecode, itemsize, buffer_info ()

 
# import & quot; array & quot; for array operations

import array

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

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

 
# use typecode to print array data type

print ( "The datatype of array is:" , end = "")

print (arr.typecode)

 
# using itemsize to print the itemsize array

print ( " The itemsize of array is: " , end = " ")

print (arr.itemsize)

 
# using buffer_info () to print buffer information. 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 the argument mentioned in the array.

5. extend (arr) : — This function appends the entire array mentioned in its arguments to the specified array.

# Python code to demonstrate how it works
# count () and extend ()

 
# import & quot; array & quot; for array operations

import array

 
# initializes 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 ​​
#in initializes an array with signed integers

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

 
# using count () to count 1 occurrences in an array

print ( "The occurrences of 1 in array is:" , end = "")

print (arr1.count ( 1 ))

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

 

print ( "The modified array is : " , end = " ")

for i in range ( 0 , 9 ):

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

Exit :

 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 add the list mentioned in its argument to the end of the array .

7. tolist () : — This function is used to convert an array to a list .

# Python code to demonstrate how it works
# fromlist () and tolist ()

 
# import & quot; array & quot; for array operations

import array

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

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

 
# initializing list

li  = [ 1 , 2 , 3 ]

 
# using fromlist () to add a list to the end of the array
arr.fromlist (li)

 
# print the modified array

print ( "The modified array is: " , end = " ")

for i in range ( 0 , 9 ):

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

 
# using tolist () to convert an array to a list

li2 = arr.tolist ()

 

print ( "" )

 
# print 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 

This article is courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.