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?

1
  • 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. geeksforgeeks.org/python-arrays Commented May 12, 2021 at 14:05

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Welcome to Stack Overflow! Please take the tour and read How to 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. Why is linking bad? | Are answers that just contain links elsewhere really “good answers”? | Your answer is in another castle: when is an answer not an answer?
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.
The link learnpython.com/blog/python-array-vs-list helps me a lot thanks.
Ok, I'll edit the answer to summarize the link.
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.

1 Comment

Thanks a lot now i understood how lists and array's store data and i started using the numpy module
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

Comments

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

2 Comments

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.
I also agree with @Shambhav Gautam

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.