DEV Community

Fardeen9065
Fardeen9065

Posted on

Array Vs List

Arrays and Lists are similar data structures. But there are some differences between arrays and lists. Lists are dynamic data structures. It means that lists can be manipulated. In other words, lists can be changed. An element can be easily added or removed in a list. For example if we want to add an element 3 in the list [7,5,1,8]. In case of list, we can easily do it using the list.append() function. But in this case the length of the list changes. Before the length of the list was 4. After the addition of 3 the length of the list now has become 5 and the list is now [7,5,1,8,3]. But this method will not work for arrays. That is because the size of the arrays are fixed. It means if we take the same array [7,5,1,8] like before then we cannot just add 3 to it. The reason is if we add 3 to the list then the size of the list will be changed. What we can do is that we can create another array setting the length of the array 1 greater than the previous array. As the length of the array is 4 we have to make another array with the length of 5. Then we will copy the previous elements in the new array. After that at the last index we can add 3. Thus, these are the differences between arrays and lists.

Top comments (0)