About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
Lists and arrays are both data structures in Python that are iterable, i.e., you can traverse the contents of the data structure element-by-element. They are useful whenever you want to model a sequence, i.e., wherever order is important, e.g., a sequence of bank transactions, a sequence of months, a sequence of house addresses, a sequence of college degrees, etc.
It is really very simple. You use square brackets to list your elements. Here’s below code to declare a list of dollar amounts and then print it.
This produces the output:
Here is code to use arrays. Arrays do not come standard in Python and we need to import this functionality from a library such as numpy.
This will produce the same output:
(Note the absence of commas.)
For a lot of purposes, you can write code that serves the same purpose using both arrays and lists.
This means that the order in which you store the elements is important and both preserve and respect the ordering. For instance, the following code
prints the first element of “a” in both examples above. In both cases it will output 1.
Mutable is just a fancy way of saying you can change their contents after initializing them. (There are data structures that are immutable; for instance, strings are immutable. After initializing a variable to a string data structure, you cannot change it.)
Here is an example with arrays:
The output will be:
Here is an example with lists:
The output will be:
This is often the confusing part. If you hide the “import numpy as np” of the code below, and the invocation of np.array, you will have no way of knowing which is a list and which is an array.
In the example below, both the list and array contain duplicate elements.
Here is an example with lists:
Here is an example with arrays:
Just as we index into lists and arrays using indices beginning at 0, we can give a range of indices - the starting and the ending index plus 1 to get a slice of the list (or array).
Here’s an example with arrays:
This outputs:
Here is an example with lists:
with the same output as above.
The above discussion might suggest that lists and arrays are equivalent. But there are numerous differences between lists and arrays in Python. Here are some of the most important differences.
This means you have to import an array library (the most common is Numpy) to get the array functionality. Recall that in the code above, we had to import the array functionality from the numpy library above.
Here’s an example of a list with elements of different types.
Note that our list, called mylist, contains strings, integers, floating point numbers, boolean, and even another list inside!
If we try to do the same with numpy:
The output is:
It looked like it worked. But note that the numpy.array method when called with a heterogeneous collection of elements has converted all of them into strings. There is no way to create numpy arrays with a heterogeneous collection. You can have all elements to be numbers or all elements to be strings. For the code below:
We get the output:
In all of the above code segments, note that we use the array constructor to define the array before doing any further operations with the array.
Consider:
This code does element-wise addition. This is not possible in lists. If you do:
It works, but notice the output:
Because arrays are declared with their full size upon initialization, arrays are optimized for storage. Lists, however, grow dynamically and therefore there is more “book-keeping” using pointers and thus Python will use more storage for lists.
Let us try growing a list first. Suppose we do:
We will get:
To grow a list, you need to explicitly append to it. For instance:
This gives:
Similarly, there is a delete method.
This yields:
There are many other useful methods to add, delete, find, replace, and update elements.
Arrays, on the other hand, cannot be updated, extended. But there are useful append functions in libraries like numpy that will return a new array (not update the existing array). For instance:
This yields:
Note that the original array "a" is not modified.
Lists are more versatile because they can store elements of different types and can grow and shrink dynamically. So if you need to store dynamic, heterogeneous, collections use lists.
In general if you are going to make heavy use of mathematical operations, or need to store and process a large amount of numerical data, you should go with arrays rather than lists. If you are also particular about efficient memory storage, you should use arrays.
Like to learn more about lists and arrays in Python? You will like our tutorials on how to copy Lists in Python and how Python sets work. Also learn what an index of -1 means in Python lists. See also our blogpost on Python's enumerate() capability and a more in-depth dive into empty Python lists. Finally, learn how to print Pascal's triangle in Python!
Want to learn Python with us? Sign up for 1:1 or small group classes.
a = [1,2,3,4]
print(a)
[1, 2, 3, 4]
import numpy as np;
a = np.array([1,2,3,4])
print(a)
[1 2 3 4]
print(a[0])
import numpy as np;
a = np.array([1,2,3,4])
a[0] = 5
print(a)
[5 2 3 4]
a = [1,2,3,4]
a[0] = 5
print(a)
[5, 2, 3, 4]
a = [2,2,3,4]
print(a)
import numpy as np;
a = np.array([2,2,3,4])
print(a)
import numpy as np;
a = np.array([2,2,3,4])
print(a[1:3])
[2 3]
a = [2,2,3,4]
print(a[1:3])
mylist = ['Kodeclik', 123, 'Washington, D.C.', True, 567.96, [3,4,5]]
print(mylist)
import numpy as np;
y = np.array([3,4.5,'Hello'])
print(y)
['3' '4.5' 'Hello']
import numpy as np;
x = np.array([3,4,5])
y = np.array(['hello','kodeclik'])
print(x)
print(y)
[3 4 5]
['hello' 'kodeclik']
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([4,5,6])
print(a+b)
a = [1,2,3]
b = [4,5,6]
print(a+b)
[1, 2, 3, 4, 5, 6]
x = [1,2]
x[2] = 3
print(x)
x[2] = 3
IndexError: list assignment index out of range
x.append(3)
[1, 2, 3]
x = [1,2]
x.append(3)
del x[0]
print(x)
[2, 3]
import numpy
a = numpy.array([1, 2, 3])
newa = numpy.append(a, [4,5,6])
print(newa)
print(a)
[1 2 3 4 5 6]
[1 2 3]