Bird
Raised Fist0
NumPydata~20 mins

np.array() from Python lists in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
np.array() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output of np.array() with nested lists
What is the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
A(2, 2)
B(4,)
C(1, 4)
D(2,)
Attempts:
2 left
💡 Hint
Think about how many rows and columns the nested list has.
❓ data_output
intermediate
1:30remaining
Data type of np.array from mixed Python lists
What is the data type of the numpy array created by this code?
import numpy as np
arr = np.array([1, 2.5, 3])
print(arr.dtype)
NumPy
import numpy as np
arr = np.array([1, 2.5, 3])
print(arr.dtype)
Abool
Bint64
Cfloat64
Dobject
Attempts:
2 left
💡 Hint
Numpy converts all elements to the same type, choosing the one that can hold all values.
🔧 Debug
advanced
2:00remaining
Error when creating np.array from irregular nested lists
What error does this code raise?
import numpy as np
arr = np.array([[1, 2], [3, 4, 5]])
print(arr)
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4, 5]])
print(arr)
AValueError: setting an array element with a sequence
BSyntaxError
CTypeError: unhashable type: 'list'
DVisible array with shape (2,)
Attempts:
2 left
💡 Hint
Check how numpy handles nested lists with different lengths.
🚀 Application
advanced
1:30remaining
Creating a 3D numpy array from Python lists
Which option creates a numpy array with shape (2, 2, 2)?
Anp.array([[[1,2],[3,4]], [[5,6],[7,8]]])
Bnp.array([[1,2],[3,4],[5,6],[7,8]])
Cnp.array([1,2,3,4,5,6,7,8])
Dnp.array([[1,2,3],[4,5,6]])
Attempts:
2 left
💡 Hint
Count the number of nested brackets and elements per level.
🧠 Conceptual
expert
2:00remaining
Why does np.array() sometimes create dtype=object?
Why does numpy create an array with dtype=object when given certain Python lists?
ABecause the Python list contains only integers.
BBecause the nested lists have different lengths or types, so numpy cannot create a uniform numeric array.
CBecause numpy always defaults to dtype=object for all Python lists.
DBecause numpy does not support nested lists.
Attempts:
2 left
💡 Hint
Think about numpy's need for uniform data types and shapes.

Practice

(1/5)
1. What does np.array() do when given a Python list?
easy
A. It converts the list into a numpy array for easier math operations.
B. It changes the list into a Python dictionary.
C. It sorts the list in ascending order.
D. It removes duplicates from the list.

Solution

  1. Step 1: Understand the purpose of np.array()

    np.array() is designed to convert Python lists into numpy arrays.
  2. Step 2: Recognize the benefit of numpy arrays

    Numpy arrays allow fast, element-wise math operations that lists do not support.
  3. Final Answer:

    It converts the list into a numpy array for easier math operations. -> Option A
  4. Quick Check:

    np.array() converts lists to arrays [OK]
Hint: np.array() turns lists into arrays for math [OK]
Common Mistakes:
  • Thinking np.array() sorts the list
  • Confusing arrays with dictionaries
  • Assuming it removes duplicates
2. Which of the following is the correct syntax to create a numpy array from a Python list [1, 2, 3]?
easy
A. np.array{1, 2, 3}
B. np.array[[1, 2, 3]]
C. np.array(1, 2, 3)
D. np.array([1, 2, 3])

Solution

  1. Step 1: Recall correct function call syntax

    np.array() requires parentheses and a list inside, like np.array([1, 2, 3]).
  2. Step 2: Identify syntax errors in other options

    np.array{1, 2, 3} uses curly braces which is invalid; C passes multiple arguments instead of one list; D uses double brackets incorrectly.
  3. Final Answer:

    np.array([1, 2, 3]) -> Option D
  4. Quick Check:

    np.array() needs a list inside parentheses [OK]
Hint: Use parentheses and a list inside for np.array() [OK]
Common Mistakes:
  • Using curly braces instead of parentheses
  • Passing multiple arguments instead of a single list
  • Using double brackets incorrectly
3. What will be the output of this code?
import numpy as np
lst = [[1, 2], [3, 4]]
arr = np.array(lst)
print(arr.shape)
medium
A. (1, 4)
B. (4,)
C. (2, 2)
D. Error

Solution

  1. Step 1: Understand the input list shape

    The list has 2 sublists, each with 2 elements, so shape is 2 rows and 2 columns.
  2. Step 2: Check numpy array shape property

    np.array converts nested lists into 2D arrays with shape (2, 2).
  3. Final Answer:

    (2, 2) -> Option C
  4. Quick Check:

    Nested list shape = (2, 2) [OK]
Hint: Shape matches nested list dimensions [OK]
Common Mistakes:
  • Thinking shape is (4,)
  • Assuming shape is (1, 4)
  • Expecting an error for nested lists
4. What is wrong with this code snippet?
import numpy as np
lst = [1, 2, 3]
arr = np.array(lst, 5)
print(arr)
medium
A. The list contains integers which are not allowed.
B. np.array() does not take a second positional argument like 5.
C. The print statement is missing parentheses.
D. The list must be nested to convert to an array.

Solution

  1. Step 1: Check np.array() function signature

    np.array() takes the data as the first argument; a second positional argument like 5 is invalid.
  2. Step 2: Identify the error caused by extra argument

    Passing 5 as a second positional argument causes a TypeError.
  3. Final Answer:

    np.array() does not take a second positional argument like 5. -> Option B
  4. Quick Check:

    Extra positional arguments cause error [OK]
Hint: np.array() takes one main argument: the list [OK]
Common Mistakes:
  • Thinking list must be nested
  • Forgetting print() needs parentheses (Python 3)
  • Believing integers are not allowed in arrays
5. You have a list of lists representing daily temperatures for a week:
temps = [[20, 22, 19], [21, 23, 20], [19, 21, 18], [22, 24, 21], [20, 22, 19], [18, 20, 17], [21, 23, 20]]

How can you convert this into a numpy array and find the average temperature for each day?
hard
A. import numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.mean(axis=1) print(daily_avg)
B. import numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.mean(axis=0) print(daily_avg)
C. import numpy as np daily_avg = np.array(temps).sum() print(daily_avg)
D. import numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.max(axis=1) print(daily_avg)

Solution

  1. Step 1: Convert list of lists to numpy array

    Use np.array(temps) to create a 2D array where each row is a day.
  2. Step 2: Calculate average temperature per day

    Use mean(axis=1) to compute average across columns (hours) for each row (day).
  3. Final Answer:

    import numpy as np temps_arr = np.array(temps) daily_avg = temps_arr.mean(axis=1) print(daily_avg) -> Option A
  4. Quick Check:

    mean(axis=1) averages rows (days) [OK]
Hint: Use mean(axis=1) to average rows (days) [OK]
Common Mistakes:
  • Using mean(axis=0) which averages columns (hours)
  • Using sum() without axis for average
  • Using max() instead of mean()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard