Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
set() Function in python
Next article icon
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 operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])# using typecode to print datatype of arrayprint("The datatype of array is : ")print(arr.typecode)

Output
The datatype of array is : i

Array itemsize Function

This function returns thesize in bytes of asingle 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 operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])# using itemsize to print itemsize of arrayprint("The itemsize of array is : ")print(arr.itemsize)

Output
The itemsize of array is : 4

buffer_info() in Python

Returns a tuple representing theaddress 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 operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])# using buffer_info() to print buffer info. of arrayprint("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 occurrencesof argument mentioned in array.

extend() in Python

This functionappends 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 operationsimportarray# initializing array with array valuesarr1=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 1arr1.extend(arr2)print("The modified array is : ")foriinrange(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 toappend 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 operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])li=[1,2,3]# using fromlist() to append list at end of arrayarr.fromlist(li)# printing the modified arrayprint("The modified array is : ",end="")foriinrange(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 operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])# using tolist() to convert array into listli2=arr.tolist()# printing the new listprint("The new list created is : ",end="")foriinrange(0,len(li2)):print(li2[i],end=" ")

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

Arrays - Part 2 Python Programming
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp