Array in Python | Set 2 (Important Functions) - GeeksforGeeks

13 May, 2020
Array in Python | Set 1 (Introduction and 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 operationsimport array   # initializing array with array values# initializes array with signed integersarr= array.array('i',[1, 2, 3, 1, 2, 5])   # using typecode to print datatype of arrayprint ("The datatype of array is : ")print (arr.typecode)  # using itemsize to print itemsize of arrayprint ("The itemsize of array is : ")print (arr.itemsize)  # using buffer_info() to print buffer info. of arrayprint ("The buffer info. of array is : ")print (arr.buffer_info())
The datatype of array is : 
i
The itemsize of array is : 
4
The buffer info. of array is : 
(29784224, 6)

Instead of import array, we can also use * to import array.
# importing "array" using * for array operationsfrom array import *   # initializing array with array values# initializes array with signed integersarr = array('i',[1, 2, 3, 1, 2, 5])   print(arr)
array('i', [1, 2, 3, 1, 2, 5])

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 operationsimport array   # initializing array 1 with array values# initializes array with signed integersarr1 = array.array('i',[1, 2, 3, 1, 2, 5])   # initializing array 2 with array values# initializes array with signed integersarr2 = array.array('i',[1, 2, 3])   # using count() to count occurrences of 1 in arrayprint ("The occurrences of 1 in array is : ")print (arr1.count(1))  # 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])
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 operationsimport array   # initializing array with array values# initializes array with signed integersarr = array.array('i',[1, 2, 3, 1, 2, 5])   # initializing listli = [1, 2, 3]  # using fromlist() to append list at end of arrayarr.fromlist(li)  # printing the modified arrayprint ("The modified array is : ",end="")for i in range (0,9):    print (arr[i],end=" ")  # using tolist() to convert array into listli2 = arr.tolist()  print ("\r")  # printing the new listprint ("The new list created is : ",end="")for i in range (0,len(li2)):    print (li2[i],end=" ")
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

YouTubeGeeksforGeeks507K subscribersPython Programming Tutorial | Arrays - Part 2 | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:42•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=21dgNSzG2Iw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
utkarsh_malik31
Python
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python Dictionary
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects
Interfaces in Java