While specific data structures (Lists, Tuples, Sets, and Dictionaries) have their own unique methods, Python provides a set of Basic Operations that can be performed across almost all of them. These operations allow you to analyze, compare, and manipulate collections efficiently.
Length (len()): Returns the total number of items in a collection.
Membership (in, not in): Checks if a specific element exists within the structure.
Iteration: Using loops to traverse through the data.
Concatenation & Repetition: Joining two structures or repeating them (primarily for Lists and Tuples).
Min/Max/Sum: Finding the smallest or largest value, or calculating the total (for numeric data).
You can often convert one data structure into another to gain specific advantages. For example, converting a List to a Set to remove duplicates, or a List of Tuples into a Dictionary.
These operations work based on the ordered nature of the sequence.
list_a = [10, 20, 30] list_b = [40, 50] # 1. Concatenation (+) combined = list_a + list_b print(f"Combined: {combined}") # [10, 20, 30, 40, 50] # 2. Repetition (*) repeated = list_a * 2 print(f"Repeated: {repeated}") # [10, 20, 30, 10, 20, 30] # 3. Membership testing print(20 in list_a) # True print(100 not in list_a) # True
These built-in functions provide quick statistics on your data structures.
numbers = [5, 12, 8, 21, 3] print(f"Total items: {len(numbers)}") # 5 print(f"Highest value: {max(numbers)}") # 21 print(f"Lowest value: {min(numbers)}") # 3 print(f"Sum of all: {sum(numbers)}") # 49
This is one of the most practical "basic operations" in day-to-day coding.
# Removing duplicates from a list raw_data = [1, 2, 2, 3, 4, 4, 4, 5] unique_data = list(set(raw_data)) print(f"Unique List: {unique_data}") # [1, 2, 3, 4, 5] # Converting a list of tuples to a dictionary pairs = [("id", 101), ("status", "Active")] data_dict = dict(pairs) print(data_dict) # {'id': 101, 'status': 'Active'}
| Operation | Syntax | Works on... |
| Length | len(s) | All structures |
| Membership | x in s | All structures |
| Concatenation | s1 + s2 | Lists, Tuples, Strings |
| Identity | id(s) | All objects |
| Clear | s.clear() | Lists, Sets, Dictionaries |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION