Junior — Middle
What types of data can subset operations be performed on?
sobes.tech AI
Answer from AI
In Python, the operation of extracting a subset of elements (slices, indexing) can be performed on the following data types:
- Lists (list) — for example,
my_list[1:4]returns elements from the second to the fourth. - Tuples (tuple) — work the same way as lists but are immutable.
- Strings (str) — a substring can be obtained using slicing, for example,
my_str[:5]. - Arrays (array.array) — support slicing.
- Sets (set) — do not support slicing because they are unordered.
- Dictionaries (dict) — do not support slices directly, but subsets of keys or values can be extracted using methods.
- NumPy arrays (numpy.ndarray) — support complex indexing and slicing.
Example of list slicing:
my_list = [10, 20, 30, 40, 50]
subset = my_list[1:4] # [20, 30, 40]
Thus, the operation of extracting a subset of elements applies to ordered and indexable data types.