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

Python course with 57 real-time projects - Learn Python

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

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.

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

How to Import Python Array Module?

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

>>> import array

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.

Let’s try creating an array in Python.

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

Output

array(‘i’, [1, 3, 4])
>>> array.array('u', 'hello \u2641')

Output

array(‘u’, ‘hello ‘)

Python Array Class – Data Items

The class array has the following data items-

Python Array Class - Data Items

Python Arrays Class – Data Items

1. array.typecodes

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

>>> array.typecodes

Output

‘bBuhHiIlLqQfd’

2. array.typecode

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

>>> arr.typecode

Output

‘i’

3. array.itemsize

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

>>> arr.itemsize

Output

4

Python Array Class – Methods

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

Python Array Class - Methods

Python Array Class – Methods

1. array.append(x)

This appends the item x to the array.

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

Output

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

2. 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()

Output

(43560864, 4)

3. array.byteswap()

This performs an operation of bytes wap on an array.

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

Output

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

4. 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)

Output

2

5. array.extend(iterable)

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

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

Output

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']))

Output

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

6. array.fromlist(list)

This appends item from a list to the Python arrays.

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

Output

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

7. 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

Output

array(‘u’, ‘Hello’)
>>> unicodearr.fromunicode(' world')
>>> unicodearr

Output

array(‘u’, ‘Hello world’)

8. 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)

Output

1

9. array.insert(I,x)

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

Output

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

This inserts the element 7 at index 2.

10. array.pop(i)

This lets us drop the element at the position i.

>>> arr.pop(2)

Output

7

11. array.remove(x)

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

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

Output

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

12. array.reverse()

This reverses the Python array.

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

Output

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

13. 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()

Output

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

14. array.tolist()

This converts the array into a list.

>>> arr.tolist()

Output

[5, 3, 4, 2, 1]

15. array.tounicode()

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

>>> unicodearr.tounicode()

Output

‘Hello world’

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

1. How to Index an Array in Python?

>>> arr

Output

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

Output

3

2. Slice an array

>>> arr[1:4]

Output

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

3. Concatenate two arrays in Python

>>> arr+arr

Output

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

4. Multiply an array by a constant

>>> arr*2

Output

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

Python Interview Questions on Array Module

  1. What is array module in Python?
  2. How do you display an array in Python?
  3. How to use an array module in Python?
  4. What is the difference between array and list in Python?
  5. How to read an array in Python?

Conclusion

Hence, while Python does not have arrays as a primary data structure, it does provide a module to let us work with arrays.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

6 Responses

  1. Udayanga Rukshan says:

    this was so helpful to me understand about arrays also this can be done by myself so easily thank you for this tutorial <3 🙂

    • DataFlair Team says:

      Hey Udayanga Rukshan,

      I am glad that you liked our tutorial, you can also refer our sidebar for more such Python tutorials.

  2. Ankush says:

    How to store characters using python array module ?

  3. Sonja Thompson says:

    3. array.byteswap()
    This performs an operation of bytes wap on an array.

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

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

    I think you meant to say “byteswap” and it’s a pretty advanced topic that seems out of place here…I’d take it out.

    • DataFlair says:

      Yes, it should be byteswap. We included this function so that we can cover as many methods on the arrays as possible. This might be helpful for someone searching for this functionality. Thank you for correcting us and also for the suggestion.

Leave a Reply

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