0

Why does python have a specific module for arrays?

I know that arrays can only hold one datatype and Python has a module for array's but why does python have a specific module for array's because if we want we can create a list with the same datatype.

So what it the actual advantage of having a array module in python?

This is a array:

from array import *

a = array("i", [5, 8])
print(a)

I can do the same exact thing with a list:

a = [5, 8]
print(a)

What the advantage with the python module array?

Arkodeep Ray
  • 174
  • 14
  • According to lovely GeeksForGeeks. Array can be handled in Python by a module named array. They can be useful when we have to manipulate only a specific data type values. A user can treat lists as arrays. However, user cannot constraint the type of elements stored in a list. If you create arrays using the array module, all elements of the array must be of the same type. https://www.geeksforgeeks.org/python-arrays/ – Buddy Bob May 12 '21 at 14:05

4 Answers4

1

I think this explains it well. https://learnpython.com/blog/python-array-vs-list/

Some key reasons to use arrays instead of lists:

  1. Arrays store information more compactly, making them more efficient and powerful.
  2. Arrays are good for numerical operation.

Some key reasons to use lists instead of arrays:

  1. Lists are good for grouping together multiple data types.
  2. Lists are mutable, meaning that they can be changed.

Though lists and arrays might have similar features, they are used for different cases.

TheChicken4452
  • 93
  • 1
  • 10
  • 2
    Welcome to Stack Overflow! Please take the [tour] and read [answer]. Link-only answers are bad! You should summarize the linked page in your answer here so your answer doesn't become useless in case of [link-rot](https://en.wikipedia.org/wiki/Link_rot). [Why is linking bad?](//meta.stackexchange.com/q/7515/174780) | [Are answers that just contain links elsewhere really “good answers”?](//meta.stackexchange.com/q/8231/174780) | [Your answer is in another castle: when is an answer not an answer?](//meta.stackexchange.com/q/225370/174780) – Pranav Hosangadi May 12 '21 at 14:34
  • 2
    Please also note that when you find a question that this one is a duplicate of, the recommended action is to flag / vote to close this question as a duplicate. You should not add an answer linking the duplicate question. – Pranav Hosangadi May 12 '21 at 14:35
  • 1
    The link https://learnpython.com/blog/python-array-vs-list/ helps me a lot thanks. – Arkodeep Ray May 12 '21 at 15:01
  • Ok, I'll edit the answer to summarize the link. – TheChicken4452 May 12 '21 at 16:13
1

Lists in Python refer to linked lists, where one member stores also stores where the next member of the list should be. Basically, member1 stores where member2 is in the memory and member2 stores where member3 is and so on.

While arrays are a big chunk of continuous memory. It is of the same datatype as jumping from the first member to other requires knowledge of the exact gap between them in memory which is only possible when the datatype is the same or of the same size. Having arrays with different datatypes will be massively inefficient.

It's easier to add or remove elements/members from a List but it is faster to parse an array. It's a tradeoff.

Python is mainly used for data science, which means elements are frequently added and removed, so arrays are a second choice hidden in a module.

Btw, nobody uses array module, just use numpy.

Shambhav
  • 813
  • 7
  • 20
0

For most cases use cases a List will do. The concept of array in Python may be confusing for those coming from other Programming language backgrounds.

That array module is wrapper over C arrays, and can only be used for specific types, whereas lists can be used for any object.

If you're not into numerical computation, and ain't using scientific packages such as scipy and numpy, I see no reason to use the array Module

Mab
  • 412
  • 3
  • 12
0

List is better , But if you're storing a large number of data, you can use array. Because arrays will store your data more compactly and efficiently

  • 1
    You can't just say "List is better", it depends upon the specific use case. In most of the cases, using arrays will be better because they are faster in most cases. I'm not saying arrays are better, I'm just saying arrays are faster in most use cases. – Shambhav May 12 '21 at 14:26
  • I also agree with @Shambhav Gautam – Arkodeep Ray May 12 '21 at 15:04