Describe the main built-in data structures and collections used in Python for storing and processing information
sobes.tech AI
Answer from AI
In Python, there are several built-in data structures and collections that are widely used for storing and processing information:
-
list — an ordered, mutable collection of elements. Allows storing elements of different types, supports indexing, slicing, and methods for adding/removing.
-
tuple — an ordered, immutable collection of elements. Used for storing fixed sets of data.
-
dict — a dictionary that stores key-value pairs. Keys must be hashable, values can be any objects. Very efficient for quick key-based lookups.
-
set — an unordered collection of unique elements. Supports union, intersection, and difference operations.
-
frozenset — an immutable version of a set.
Example usage:
fruits = ['apple', 'banana', 'cherry'] # list
coordinates = (10.0, 20.0) # tuple
phonebook = {'Alice': '+123456', 'Bob': '+987654'} # dict
unique_numbers = {1, 2, 3, 2} # set, result: {1, 2, 3}
These structures allow for efficient data organization and various operations on them.