Array in Python | Set 2 (Important Functions)
Last Updated :
06 Sep, 2023
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)
OutputThe 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)
OutputThe 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())
OutputThe 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=" ")
OutputThe 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=" ")
OutputThe 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=" ")
OutputThe new list created is : 1 2 3 1 2 5
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read