# What are some differences between an array and a list?

### Question

What are some differences between an `array` and a `list`?

In Python, an array, or NumPy Array, and list share many similarities, but they also have some important differences on how they can be used.

Both arrays and lists can hold multiple items of any type. You can also access individual items by indexes.

One important, and probably the main difference, between them is that you can perform operations on an array, like addition, multiplication, and subtraction, like you would a vector in mathematics.

This means that if you have an array of numbers, you can add a single number to every value in the array with one operation. With a list, operations cannot be applied on every single element like for an array, and might even cause errors.

Arrays also provide several other useful functionality, which you will learn throughout this lesson.

#### Example

``````test = np.array([1, 2, 3, 4, 5])
test += 10

# The array is now [11, 12, 13, 14, 15]
``````
8 Likes

I was attempting to do a comparison between lists and arrays so I carried out the below but for some reason it didnâ€™t work, any ideas what Iâ€™ve done wrong?

``````import numpy as np
test_1 = np.array([92, 94, 88, 91, 87])
a = [1,2,3,4,1,2]
m = list (lambda a : a + 2)
print (m)
``````

here you define a lambda function:

``````lambda a : a + 2
``````

then you try to convert the lambda function to list, why?

you are never call the lambda function

i thought because it was originally a list it needed to be converted to a list. I tried the below modified on your suggestion but itâ€™s still not working. When you say you never call the lambda what do you mean?
revised code:

``````import numpy as np
test_1 = np.array([92, 94, 88, 91, 87])
b= [1,2,3,4,1,2]
m = lambda b : b + 2
print (m)
``````

what is `it`? `b` variable?

lambdas are function, first you define the function. Then in order to execute a function you need to call the function/lambda.

what is exactly you are trying to achieve?

I wanted to add 2 to every entry in the list b

so you defined the (lambda) function, what is stopping you from calling the (lambda) function?

sorry I donâ€™t understand what you mean? When i execute the above i was expecting to see a list but with all the elements increased by two but instead I get this <function at 0x7ff37d9e3de8>

why did you expect a list? You defined the function, you never called/executed the function.

https://www.tutorialspoint.com/python/python_functions.htm

The great thing about functions is that we can define them, and then call them multiple times to prevent repetitive code.

i am sorry, but understanding the difference between â€śdefiningâ€ť and calling a function is quite a fundamental part of functions.

if you use lambdas, one might assume you are at least familiar with the fundamentals of functions

is also told you what is wrong, which can help you in researching a solution.

A lambda function is a function, just as is a function defined using def. After being defined, it can be called. If you call it without providing any arguments, the actual function object will be returned. If you call it as you generally call any other function, with appropriate arguments, it will do what you expect:

``````m = lambda x: x** 2
print(m)
p = m(6)
print(p)
``````

Output

``````<function <lambda> at 0xabcdef123456>
36
``````

It is possible that you are used to seeing lambda functions â€śstanding aloneâ€ť in key = expressions within obj.sort() or other container (e.g., list) methods. In that useage, the key = functions as sort of an â€śiterative operatorâ€ť that calls the lambda on each item in the container.

6 Likes

Food for thoughtâ€¦

``````>>> b= [1,2,3,4,1,2]
>>> m = map(lambda x : x + 2, b)
>>> m
[3, 4, 5, 6, 3, 4]
>>>
``````

We see above how the `map` iterator is utilized to produce the desired outcome. `map` takes a callback, but `list` does not.

7 Likes

Thank you @patrickd314 that was very helpful

2 Likes

Since you are on the topic of lambda functions, Iâ€™d like to know if anyone could explain to me the term â€śanonymousâ€ť or â€śnamelessâ€ť function. So we are supposed to be using lambda functions when we need a anonymous or nameless function for a short period of time, but how are lambda functions nameless? You still assign the lambda function to a variable, and the function will be called by the name of that variable, hence not nameless at all? The only difference I can see between a â€śfullâ€ť function and a lambda function is that the latter can be written within one line and hence is a neat solution for simple functions.

Another thing I wanted to ask about here is about the arrays in numpy. So arrays are mutable just like lists, right? But why in the exercises for the numpy modules it is rarely the case that the original array get altered? Instead, most often there will be a new array being defined. I wonder why this is the prefered solution. Is it simply because in this way both new and old data will be preserved?

Python Lists vs. Numpy Arrays - What is the difference?: IST Advanced Topics Primer

Since they are not equivalent data structures it makes sense that the list is not mutated. The data is only copied over to the Numpy array.

1 Like