Advertisement



< Prev
Next >



Python - Array from Array Module





Python language provides us a capability by which we could hold together a set of similar data types, through its feature known as array. In other words, an array allows us to hold multiple values of a same type.




Creating an array in Python


To allow us create an array, Python provides us a special module named array. To create an array, we need to import this array module and call its array function. Let us look at the syntax of this function.

array(typecode, initializer)

While calling the array() function, we need to pass two values - typecode and initializer(a list of values that we are initializing an array with). Let us see one of many values of typecode that we could pass to the array() function.

Type Code C Type Python Type Minimum Size in Bytes
'b' signed char
int
1
'B' unsigned char
int
1
'b' signed char
int
1
'u' Py_UNICODE
Unicode Character
2
'h' signed short
int
2
'H' unsigned char
int
2
'i' signed int
int
2
'I' unsigned char
int
2
'l' signed long
int
4
'L' unsigned long
int
4
'f' float
float
4
'd' double
float
8






# Python  - Example of an array 


import array as arr


# Creating an array of type int
first_array = arr.array('i', [1, 5,  24, 4])



# Creating an iterable list
a_list = [ 5,7,2,2,1]


# Creating a iterable tuple
a_tuple =( 27, 17)



# Creating a iterable set
a_set ={99, 21}



# Printing the priginal first array, list, tuple and set 
print('First array : ',first_array)
print('List : ', a_list)
print('Tuple : ', a_tuple)
print('Set : ', a_set)



# Calling the append() function to append the list to the end of first array
first_array.extend(a_list)



# Printing the modified first array, after extending it to add a list
print('Extended first array with a list : ', first_array)



# Calling the append() function to append the tuple to the end of first array
first_array.extend(a_tuple)



# Printing the modified first array, after extending it to add a tuple
print('Extended first array with a tuple : ', first_array)



# Calling the append() function to append the set to the end of first array
first_array.extend(a_set)



# Printing the modified first array, after extending it to add a set
print('Extended first array with a set : ', first_array)

Output


First array :  array('i', [1, 5, 24, 4])
List :  [5, 7, 2, 2, 1]
Tuple :  (27, 17)
Set :  {99, 21}
Extended first array with a list :  array('i', [1, 5, 24, 4, 5, 7, 2, 2, 1])
Extended first array with a tuple :  array('i', [1, 5, 24, 4, 5, 7, 2, 2, 1, 27, 17])
Extended first array with a set :  array('i', [1, 5, 24, 4, 5, 7, 2, 2, 1, 27, 17, 99, 21])





Array v/s List


While both the array and list are used to contain multiple elements but there is some difference between the two, let us read these differences.

Note :


Besides using the array module to create and work with an array, Python also provides us another module named numpy, which also provides us several important functions to create an array and perform several array related operations and we are going to discuss just that in our next tutorial.



Please share this article -





< Prev
Next >
< Python garbage collection
Python array from numpy module >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement