Python

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.

1. Theoretical Overview

Common Universal Operations

  1. Length (len()): Returns the total number of items in a collection.

  2. Membership (in, not in): Checks if a specific element exists within the structure.

  3. Iteration: Using loops to traverse through the data.

  4. Concatenation & Repetition: Joining two structures or repeating them (primarily for Lists and Tuples).

  5. Min/Max/Sum: Finding the smallest or largest value, or calculating the total (for numeric data).

Type Conversion (Casting)

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.

2. Code Implementation

A. Sequence Operations (Lists & Tuples)

These operations work based on the ordered nature of the sequence.

Python
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

B. Aggregate Functions

These built-in functions provide quick statistics on your data structures.

Python
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

C. Structure Conversion

This is one of the most practical "basic operations" in day-to-day coding.

Python
# 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'}

3. Operations Summary Table

OperationSyntaxWorks on...
Lengthlen(s)All structures
Membershipx in sAll structures
Concatenations1 + s2Lists, Tuples, Strings
Identityid(s)All objects
Clears.clear()Lists, Sets, Dictionaries
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now