Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Array indexing and slicing in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of slicing a 2D NumPy array
What is the output of the following code snippet?
Data Analysis Python
import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
sliced = arr[1:, :2]
print(sliced)
A
[[50 60]
 [80 90]]
B
[[40 50]
 [70 80]]
C
[[20 30]
 [50 60]]
D
[[40 60]
 [70 90]]
Attempts:
2 left
💡 Hint
Remember that arr[1:, :2] means rows from index 1 to end, columns from index 0 up to 2 (not including 2).
❓ data_output
intermediate
1:30remaining
Number of elements after slicing a 1D array
Given the array arr = [5, 10, 15, 20, 25, 30], how many elements are in arr[2:5]?
Data Analysis Python
arr = [5, 10, 15, 20, 25, 30]
sliced = arr[2:5]
print(len(sliced))
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Remember slicing includes the start index but excludes the end index.
🔧 Debug
advanced
2:00remaining
Identify the error in slicing a NumPy array
What error does the following code raise?
Data Analysis Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
sliced = arr[::2, 1]
print(sliced)
ASyntaxError: invalid syntax
BTypeError: unhashable type: 'slice'
CIndexError: too many indices for array
DNo error, outputs [1 3 5]
Attempts:
2 left
💡 Hint
Check how many dimensions the array has and how many indices are used.
🧠 Conceptual
advanced
2:00remaining
Effect of negative step in slicing
What is the output of this code? import numpy as np arr = np.array([10, 20, 30, 40, 50]) sliced = arr[4:1:-1] print(sliced)
A[50 40 30 20]
B[40 30 20]
C[10 20 30]
D[50 40 30]
Attempts:
2 left
💡 Hint
Negative step means slicing backwards, start index is included, end index is excluded.
🚀 Application
expert
2:30remaining
Extracting a diagonal from a 2D NumPy array using slicing
Given a 3x3 NumPy array, which slicing expression extracts the main diagonal elements as a 1D array?
Data Analysis Python
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
Aarr[range(3), range(3)]
Barr[:, 0]
Carr[0:3, 0:3]
Darr[::2, ::2]
Attempts:
2 left
💡 Hint
Think about how to select elements where row and column indices are the same.

Practice

(1/5)
1. What does the expression arr[2] do when arr is a Python list?
easy
A. Returns the first two elements of the list
B. Removes the element at index 2 from the list
C. Returns a new list with elements from index 0 to 2
D. Returns the element at index 2 (third element) of the list

Solution

  1. Step 1: Understand list indexing

    In Python, list indexing starts at 0, so index 2 means the third element.
  2. Step 2: Identify what arr[2] returns

    This expression returns the single element at position 2, not a range or removal.
  3. Final Answer:

    Returns the element at index 2 (third element) of the list -> Option D
  4. Quick Check:

    Index 2 means third element [OK]
Hint: Indexing gets one element by position [OK]
Common Mistakes:
  • Thinking slicing syntax is used for single element
  • Confusing index 2 with first two elements
  • Assuming it removes an element
2. Which of the following is the correct syntax to get a slice of a list lst from index 1 to 4 (excluding 4)?
easy
A. lst(1:4)
B. lst[1:4]
C. lst[1,4]
D. lst[1-4]

Solution

  1. Step 1: Recall Python slice syntax

    Slicing uses square brackets with colon: start:stop, where stop is excluded.
  2. Step 2: Check each option

    Only lst[1:4] uses correct slice syntax to get elements at indices 1, 2, 3.
  3. Final Answer:

    lst[1:4] -> Option B
  4. Quick Check:

    Slice syntax uses colon [OK]
Hint: Use colon inside brackets for slicing [OK]
Common Mistakes:
  • Using comma instead of colon
  • Using parentheses instead of brackets
  • Using dash instead of colon
3. Given arr = [10, 20, 30, 40, 50], what is the output of arr[1:4]?
medium
A. [20, 30, 40]
B. [10, 20, 30, 40]
C. [30, 40, 50]
D. [20, 30, 40, 50]

Solution

  1. Step 1: Understand slicing indices

    The slice arr[1:4] includes elements from index 1 up to but not including index 4.
  2. Step 2: Extract elements at indices 1, 2, 3

    These are 20 (index 1), 30 (index 2), and 40 (index 3).
  3. Final Answer:

    [20, 30, 40] -> Option A
  4. Quick Check:

    Slice excludes stop index [OK]
Hint: Slice includes start, excludes stop index [OK]
Common Mistakes:
  • Including element at stop index
  • Confusing indices with values
  • Off-by-one errors in slicing
4. What is wrong with this code snippet?
arr = [1, 2, 3, 4, 5]
print(arr[2:6])
medium
A. It will print elements from index 2 to 6 including index 6
B. It will cause an IndexError because 6 is out of range
C. It will print elements from index 2 to 5 without error
D. It will print only the element at index 6

Solution

  1. Step 1: Understand slicing behavior with out-of-range stop

    Python slicing does not raise error if stop index is beyond list length; it stops at the list end.
  2. Step 2: Check what arr[2:6] returns

    It returns elements from index 2 to the end (index 4), which are [3, 4, 5].
  3. Final Answer:

    It will print elements from index 2 to 5 without error -> Option C
  4. Quick Check:

    Slicing stop can exceed length safely [OK]
Hint: Slicing stop index can exceed list length safely [OK]
Common Mistakes:
  • Expecting IndexError for slicing beyond length
  • Thinking slice includes stop index
  • Confusing slicing with indexing
5. You have a list data = [5, 10, 15, 20, 25, 30]. How do you get every second element starting from index 1 up to index 5?
hard
A. data[1:6:2]
B. data[1:5:2]
C. data[1:5]
D. data[::2]

Solution

  1. Step 1: Understand slicing with step

    Slicing syntax is start:stop:step. To get every second element, step=2.
  2. Step 2: Identify correct start and stop

    Start is 1, stop is 6 to include element at index 5 (stop excluded).
  3. Step 3: Verify the slice

    data[1:6:2] returns elements at indices 1, 3, 5 -> [10, 20, 30].
  4. Final Answer:

    data[1:6:2] -> Option A
  5. Quick Check:

    Use step in slice for skipping elements [OK]
Hint: Use third slice value for step size [OK]
Common Mistakes:
  • Using wrong stop index excluding last element
  • Omitting step and getting all elements
  • Confusing step with start or stop
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