0
0
NumPydata~5 mins

np.array() from Python lists in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.array() do when given a Python list?

np.array() converts a Python list into a NumPy array, which is a grid of values all of the same type. This allows for fast math and data operations.

Click to reveal answer
beginner
How do you create a 2D NumPy array from a list of lists?

Pass a list of lists to np.array(). Each inner list becomes a row in the 2D array.

arr = np.array([[1, 2], [3, 4]])
Click to reveal answer
beginner
What is the difference between a Python list and a NumPy array created from it?

A Python list can hold different data types and is slower for math. A NumPy array has one data type and is optimized for fast math and memory use.

Click to reveal answer
intermediate
Can np.array() handle lists with mixed data types? What happens?

Yes, but NumPy will convert all elements to a common data type, usually upcasting to a type that can hold all values (like strings if mixed types). This can affect performance.

Click to reveal answer
beginner
How can you check the shape of a NumPy array created from a list?

Use the .shape attribute to see the dimensions (rows, columns) of the array.

arr = np.array([[1, 2], [3, 4]])
print(arr.shape)  # Output: (2, 2)
Click to reveal answer
What does np.array([1, 2, 3]) return?
AA Python tuple with elements 1, 2, 3
BA Python list with elements 1, 2, 3
CAn error because input is a list
DA NumPy array with elements 1, 2, 3
How do you create a 2D NumPy array from lists?
Anp.array([[1, 2], [3, 4]])
Bnp.array([1, 2, 3])
Cnp.array(1, 2, 3, 4)
Dnp.array({1:2, 3:4})
What attribute shows the shape of a NumPy array?
A.size
B.shape
C.length
D.type
If a list has mixed types, what does np.array() do?
AConverts all to a common type
BCreates a list instead
CThrows an error
DKeeps original types in array
Why use NumPy arrays instead of Python lists?
ANumPy arrays are slower
BNumPy arrays use more memory
CNumPy arrays allow fast math operations
DPython lists support only numbers
Explain how to create a NumPy array from a Python list and why it is useful.
Think about what changes when you convert a list to an array.
You got /4 concepts.
    Describe what happens if you create a NumPy array from a list with mixed data types.
    Consider how NumPy handles different types in one array.
    You got /4 concepts.