Python Array Module – How to Create and Import Array in Python

1. Python Array Module – Objective

Today in this Python Array Tutorial, we will learn about arrays in Python Programming. Here, we will discuss how Python array import module and how can we create Array. Along with this, we will cover the Python Array Class Modules and Data Items.

Python Array Module - Data Items & Modules

Python Array Module – Data Items & Modules

2. What is a Python Array Module?

Python array module gives us an object type that we can use to denote an array. This is a collection of a type of values. In a way, this is like a Python list, but we specify a type at the time of creation.
Do you Know about Python Tuples 
Here’s a list of such type codes-

Type Code

C Type

Python Type

Minimum size (bytes)

b

signed char

int

1

B

unsigned char

int

1

u

Py_UNICODE

Unicode character;

deprecated since Python 3.3

2

h

signed short

int

2

H

unsigned short

int

2

i

signed int

int

2

I

unsigned int

int

2

l

signed long

int

4

L

unsigned long

int

4

q

signed long long

int

8

Q

unsigned long long

int

8

f

float

float

4

d

double

float

8

3. How to Import Python Array Module?

If a Python array is installed on your machine, you can import it as:

>>> import array

4. How to Create a Python Array?

You can create an array using the following piece of code-

class array.array(typecode[,initializer])

This creates a new array with items of the type specified by the type code. You can optionally provide an initializer value- a list.
Read about Python Strings with Functions & Operations
Let’s try creating an array in Python.

>>> arr=array.array('i',[1,3,4])
>>> arr

array(‘i’, [1, 3, 4])

>>> array.array('u', 'hello \u2641')

array(‘u’, ‘hello ‘)

5. Python Array Class – Data Items

The class array has the following data items-

Python Array Class - Data Items

Python Arrays Class – Data Items

a. array.typecodes

This is a string with all available type codes- the ones in the table above.

>>> array.typecodes

‘bBuhHiIlLqQfd’

b. array.typecode

This gives us the type code character we used when creating the array in Python.

>>> arr.typecode

‘i’

c. array.itemsize

This returns the number of bytes one item from the Python array takes internally.

>>> arr.itemsize

4
Let’s know about Python Web Framework – A Detailed List with Explanations

6. Python Array Class – Methods

Now, which methods does Array Class support? Here you go:

Python Array Class - Methods

Python Array Class – Methods

a. array.append(x)

This appends the item x to the array.

>>> arr.append(2)
>>> arr

array(‘i’, [1, 3, 4, 2])

b. array.buffer_info()

This returns a tuple that holds the address in memory and the length of elements in the buffer that holds the contents of the array.

>>> arr.buffer_info()

(43560864, 4)

c. array.byteswap()

This performs an operation of bytes wap on an array.

>>> arr.byteswap()
>>> arr

array(‘i’, [16777216, 50331648, 67108864, 33554432])

d. array.count(x)

Let’s find out how many 3s there are in our Python array.

>>> arr=array.array('i',[1,3,2,4,3,5])
>>> arr.count(3)

2

e. array.extend(iterable)

This attaches the iterable to the end of the array in Python.

>>> arr.extend([7,9,8])
>>> arr

array(‘i’, [1, 3, 2, 4, 3, 5, 7, 9, 8])
But if you add another array, make sure it is the same type. The following code throws an error.

>>> arr.extend(array.array('u',['H','e','l','l','o']))

Traceback (most recent call last):
File “<pyshell#19>”, line 1, in <module>
arr.extend(array.array(‘u’,[‘H’,’e’,’l’,’l’,’o’]))
TypeError: can only extend with array of same kind

f. array.fromlist(list)

This appends item from a list to the Python arrays.

>>> arr.fromlist([9,0])
>>> arr

array(‘i’, [1, 3, 2, 4, 3, 5, 7, 9, 8, 9, 0])

g. array.fromunicode(s)

This appends the Unicode string to the one we call it on- this should be Unicode too.

>>> unicodearr=array.array('u','Hello')
>>> unicodearr

array(‘u’, ‘Hello’)

>>> unicodearr.fromunicode(' world')
>>> unicodearr

array(‘u’, ‘Hello world’)
Follow this link to know more about Python Itertools 

h. array.index(x)

This returns the index for the first occurrence of x in the Python array.

>>> arr=array.array('i',[1,3,2,4,3,5])
>>> arr.index(3)

1

i. array.insert(I,x)

>>> arr.insert(2,7)
>>> arr

array(‘i’, [1, 3, 7, 2, 4, 3, 5])
This inserts the element 7 at index 2.

j. array.pop(i)

This lets us drop the element at the position i.

>>> arr.pop(2)

7

k. array.remove(x)

This will let you remove the first occurrence of an element from the Python array.

>>> arr.remove(3)
>>> arr

array(‘i’, [1, 2, 4, 3, 5])

l. array.reverse()

This reverses the Python array.

>>> arr.reverse()
>>> arr

array(‘i’, [5, 3, 4, 2, 1])

m. array.tobytes()

This returns a representation in bytes of the values of the array in Python. This is the same as array.tostring(), which is deprecated.

>>> arr.tobytes()

b’\x05\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00′

n. array.tolist()

This converts the array into a list.

>>> arr.tolist()

[5, 3, 4, 2, 1]
Do you know the difference between Python Tuples and Lists 

o. array.tounicode()

This converts an array to a Unicode string. You need a Unicode array for this.

>>> unicodearr.tounicode()

‘Hello world’

Python Interview Questions

7. Python Array – More Information

Python Arrays are space-efficient collections of numeric values that are uniformly-typed. You can:

Python Array - More Information

Python Arrays Module – More Information

a. How to Index an Array in Python?

>>> arr

array(‘i’, [5, 3, 4, 2, 1])

>>> arr[1]

3

b. Slice an array

>>> arr[1:4]

array(‘i’, [3, 4, 2])

c. Concatenate two arrays in Python

>>> arr+arr

array(‘i’, [5, 3, 4, 2, 1, 5, 3, 4, 2, 1])

d. Multiply an array by a constant

>>> arr*2

array(‘i’, [5, 3, 4, 2, 1, 5, 3, 4, 2, 1])
So, this was all about Python Arrays Tutorial. Hope you like our explanation

8. Conclusion

Hence, while Python does not have arrays as a primary data structure, it does provide a module to let us work with arrays. Tell us what you think in the comments below.
Related Topic- Scala vs Python
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.