New course! Every coder should learn Generative AI!
Try a free lesson+ 2
Isnt this an array
so this is technically an array, isn't it
4 Answers
+ 3
They're similiar, but you can't change the size of an array where as elements of a list can easily be added and removed. There are also library function like sort() and reverse() for lists which can make things a lot easier.
+ 3
Oh, no. List are not arrays at all. They both are collections, but they are stored in a computer memory in a quite different way.
Arrays are stored by just an address if it's first element. The access to a certain array element is got by specifying an offset for the element from the first one as array index. E.g. we have an array of ints. Each int occupies 4 bytes in memory. To get the 5th element of array we need to add 4(length of int)x4(the offset) to the address of the first element and there will be the element we want. That's why, btw, the array indexing starts from 0. Arrays are good for random access to every element, but are not resizable, because the memory is allocated to an array when its created. If we "resize" an array, we actually create a new array and copy to it the data from an old one.
+ 1
List is a collection of elements, each of which stores an object of data and a link to next and, sometimes, a previous element. List can be easily resized, because the memory is allocated to each element separately and they can be easily iterated by links, but they are not to good for random access, because to get to the nth element of list you should go step by step from 0 to nth element.
0
I contradict the statement that lists aren't arrays. what is called as arrays in other programming languages is actually called as list in python. The concept of Data structures is same here.