Why are lists better than arrays?

List: A list in Python is a collection of items which can contain elements of multiple data types, which may be either numeric, character logical values, etc. It is an ordered collection supporting negative indexing. A list can be created using [] containing data values.
Contents of lists can be easily merged and copied using python’s inbuilt functions.

sample_list = [1,"Yash",['a','e']]

print(sample_list)

Output :

[1, 'Yash', ['a', 'e']]

The first element is an integer, the second a string and the third is an list of characters.

Array: An array is a vector containing homogeneous elements i.e. belonging to the same data type. Elements are allocated with contiguous memory locations allowing easy modification, that is, addition, deletion, accessing of elements. In Python, we have to use the array module to declare arrays. If the elements of an array belong to different data types, an exception “Incompatible data types” is thrown.

import array

sample_array = array.array('i', [1, 2, 3])  

for i in sample_array:

     print(i)

Output :

1 2 3

Here are the differences between List and Array in Python :

List Array
Can consist of elements belonging to different data types Only consists of elements belonging to the same data type
No need to explicitly import a module for declaration Need to explicitly import a module for declaration
Cannot directly handle arithmetic operations Can directly handle arithmetic operations
Can be nested to contain different type of elements Must contain either all nested elements of same size
Preferred for shorter sequence of data items Preferred for longer sequence of data items
Greater flexibility allows easy modification (addition, deletion) of data Less flexibility since addition, deletion has to be done element wise
The entire list can be printed without any explicit looping A loop has to be formed to print or access the components of array
Consume larger memory for easy addition of elements Comparatively more compact in memory size

Article Tags :

Practice Tags :

Arrays store elements in contiguous memory locations, resulting in easily calculable addresses for the elements stored and this allows faster access to an element at a specific index. Linked lists are less rigid in their storage structure and elements are usually not stored in contiguous locations, hence they need to be stored with additional tags giving a reference to the next element. This difference in the data storage scheme decides which data structure would be more suitable for a given situation. 

Why are lists better than arrays?

Data storage scheme of an array

Why are lists better than arrays?

Data storage scheme of a linked list

Major differences are listed below: 

  • Size: Since data can only be stored in contiguous blocks of memory in an array, its size cannot be altered at runtime due to the risk of overwriting other data. However, in a linked list, each node points to the next one such that data can exist at scattered (non-contiguous) addresses; this allows for a dynamic size that can change at runtime.
  • Memory allocation: For arrays at compile time and at runtime for linked lists. but, a dynamically allocated array also allocates memory at runtime.
  • Memory efficiency: For the same number of elements, linked lists use more memory as a reference to the next node is also stored along with the data. However, size flexibility in linked lists may make them use less memory overall; this is useful when there is uncertainty about size or there are large variations in the size of data elements; memory equivalent to the upper limit on the size has to be allocated (even if not all of it is being used) while using arrays, whereas linked lists can increase their sizes step-by-step proportionately to the amount of data.
  • Execution time: Any element in an array can be directly accessed with its index; however in the case of a linked list, all the previous elements must be traversed to reach any element. Also, better cache locality in arrays (due to contiguous memory allocation) can significantly improve performance. As a result, some operations (such as modifying a certain element) are faster in arrays, while some others (such as inserting/deleting an element in the data) are faster in linked lists.

Following are the points in favor of Linked Lists. 
(1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and in practical uses, the upper limit is rarely reached. 



(2) Inserting a new element in an array of elements is expensive because room has to be created for the new elements and to create room existing elements have to be shifted. 

For example, suppose we maintain a sorted list of IDs in an array id[ ]. 

id[ ] = [1000, 1010, 1050, 2000, 2040, …..]. 

And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). 

Deletion is also expensive with arrays unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved. 

So Linked list provides the following two advantages over arrays 1) Dynamic size 

2) Ease of insertion/deletion 

Linked lists have the following drawbacks: 1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do a binary search with linked lists. 2) Extra memory space for a pointer is required with each element of the list. 

3) Arrays have better cache locality that can make a pretty big difference in performance.

4) It takes a lot of time in traversing and changing the pointers.

5)  It will be confusing when we work with pointers.

References: 
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Article Tags :

Practice Tags :

Why are lists better than arrays?

Difference Between C# Array and List

C# Array vs List is wherever the abstraction and implementation of people in computing meet. An array is incredibly lot of tied to the hardware notion of continuous, contiguous memory, with every part identical in size (although typically these parts are addresses, and so talk over with non-identically-sized referents). A list could be an idea (from arithmetic to an extent) wherever parts are ordered and wherever there’s (normally) a starting and finish, and thus wherever indexing is feasible. These 2 ideas line up quite well. However, once we contemplate a list as an abstract data sort, an approach to accessing and manipulating data, we are able to break a number of those rules.

What is an Array?

An array could be a sequent assortment of comparable data, which will be accessed as per the “index”. It’s the best style of a system during which the weather get to keep in a contiguous memory location.

In Array, the index starts at zero, thus to access the primary part of An array “numarray”, it ought to be written as numarray[0].

An array could be a consecutive section of memory that occupies n*size(type) bytes, wherever n is that the length of the array and size(type) is that the size in memory needed to store the info sort you’re progressing to use within the array. This suggests that if you would like to form an array of one hundred ints, and every int occupies four bytes, you may have to be compelled to have an unused memory section of a minimum of four hundred bytes (100*4). This also implies that the array is pretty cheap to form, unleash, and use due to their chunks of memory.

Array Options:-

  • The info is kept in a type of continuous memory allocations. every half follows different simply once it within the m/y. there’s no randomness in allocation.
  • They give random access like arr[0],arr[6] etc.
  • There is a static allocation of memory. n this might result in wastage of memory.
  • There is only 1 style of data in every cell of an array.
  • Insertion and deletion are a bit longer intense.

What is a List?

The ArrayList could be an assortment of objects of the same or differing types. The dimensions of An ArrayList is dynamically inflated or slashed as per necessity. It works like an array; however, in contrast to an array in ArrayList, things are dynamically allotted or deallocated, i.e. you’ll add, remove, index, or hunt for data in a very assortment.

A list but could be an utterly completely different structure. Most list implementations are a mix of nodes that store: one. – One price and, 2. – One or a lot of pointers that keep the nodes connected between them. This suggests that you just do not want an enormous chunk of obtainable memory with a size large enough to carry all of your data because the nodes are scattered through your memory.

List options:-

  • The info is kept at random in components. n every half is connected to different via a pointer to next cell (n to the previous cell just in case of double link list)
  • They are to be accessed consecutive thanks to the dependency of every half.
  • It is dynamically allotted that’s m/y is allotted to every cell once process request for it. Thus there’s no m/y wastage.
  • A single cell is divided into several components, every having information of various data sort. However, the last essentially has to be the pointer to an ensuing cell.
  • Insertion and deletion are a ton a lot of easier and quicker. Looking out, too, is easier.

Head To Head Comparison Between C# Array and List

Below is the top 5 difference between C# Array vs List

Why are lists better than arrays?

Key Difference Between C# Array and List

Let’s look at the top Comparison between C# Array and List below –

  1. Array stores data of the same sort, whereas ArrayList stores data within the type of the object, which can be of various sorts.
  2. The size of An ArrayList grows dynamically, whereas Array size remains static throughout the program.
  3. Insertion and deletion operation in ArrayList is slower than an Array.
  4. Arrays are powerfully typewritten, whereas ArrayLists aren’t powerfully typewritten.
  5. Arrays belong to System. Array namespace whereas ArrayList belongs to System. Collections namespace.
  6. Once selecting between Array and ArrayList, opt for the idea of their options that you just need to implement.

 C# Array vs List Comparison Table

Below is the topmost comparison

S.No.

Array

List

1 Arrays are continuous in memory that makes it exhausting (in a performance sense) to insert parts within the middle of the list. The advantage is that the ability to perform random access. Lists, on the opposite hand, are parts that unfold concerning in memory, link along. This enables straightforward insertion within the list; however, random access while not further data structures isn’t doable.
2 An array could be a system; that’s to mention, it’s a particular approach to organizing data within the memory device. A list is an abstract data type; that is to say, it is any data structure that supports a specific bunch of operations.
3 An array is a collection of homogeneous parts. A list is a collection of heterogeneous elements.
4 Array memory allocated is static and continuous. List memory allocated is dynamic and Random.
5 A user needn’t ought to confine track of the next memory allocation. A user must confine Track of the next location wherever memory is allotted.

Conclusion

We saw a comparison of C# Array vs List performance memory usage within the C# language. For speed, it’s typically worthy to like regular arrays. The performance profit is critical.

Lists are used much more usually in C# than arrays are; however, there are some instances wherever arrays will (or should) be used, together with if your data is unlikely to grow significantly or if you’re coping with a comparatively great deal of data which will have to be compelled to be indexed into usually.

Let me offer you 2 samples of lists that break the principles of an array. In a link list, every part points to the ensuing part; thus, I will simply place a replacement part between 2 existing parts or take away one and fix the 2 remainings (the previous And next); whereas I will access parts via an index, I will solely do this by moving from one part to ensuring and investigating, thus it isn’t really indexed. Another example is that the queue, wherever I will solely boost the tip and take away from the beginning; if I want to access parts via an index, it’s doable; however, I’m clearly not mistreatment the proper abstract data sort. It doesn’t matter if the implementation would give this simply.

Recommended Article

This has been a guide to the top differences between C# Array vs List. Here we also discuss the key differences with infographics and comparison table. You may also have a look at the following articles –