Step 1: Python Lists
Lists are ordered, mutable collections.
# Creating a list
fruits = ["Apple", "Banana", "Cherry"] # list of strings
print(fruits[0]) # Access first element
fruits.append("Orange") # Add element at end
print(fruits)
fruits.remove("Banana") # Remove element
print(fruits)
print(len(fruits)) # Length of list
Explanation: Lists can be modified (mutable). Indexing starts from 0. Use append() to add, remove() to delete.
Removing Duplicates from Lists
Example list with duplicates:
numbers = [1, 2, 3, 2, 4, 1, 5]
Method 1: Using set()
unique_numbers = list(set(numbers)) # removes duplicates
print(unique_numbers)
Method 2: Using loop (preserves order)
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
print(unique_numbers)
Method 3: Using dict.fromkeys() (Python ≥3.7, preserves order)
unique_numbers = list(dict.fromkeys(numbers))
print(unique_numbers)
Step 2: Python Tuples
Tuples are ordered, immutable collections.
colors = ("Red", "Green", "Blue")
print(colors[1]) # Access second element
# colors.append("Yellow") # ❌ This will raise an error
print(len(colors))
Explanation: Tuples cannot be changed after creation. Use tuples when data should not be modified.
Step 3: Python Sets
Sets are unordered collections of unique elements.
numbers_set = {1, 2, 3, 4}
numbers_set.add(5) # Add element
numbers_set.add(3) # Duplicate ignored
print(numbers_set)
numbers_set.remove(2)
print(numbers_set)
Explanation: Sets are useful for removing duplicates or performing union/intersection operations.
Step 4: Python Dictionaries
Dictionaries store key-value pairs.
student = {"name": "Alisha", "age": 30, "grade": "A"}
print(student["name"]) # Access value by key
student["age"] = 31 # Update value
student["city"] = "Dallas" # Add new key-value
print(student.keys())
print(student.values())
Explanation: Dictionaries are mutable. Keys must be unique. Values can be any data type.
Full-fledged Example: Counting Occurrences
Use a dictionary to count the frequency of items in a list:
items = ["apple", "banana", "apple", "orange", "banana", "apple"]
count_dict = {}
for item in items:
if item in count_dict:
count_dict[item] += 1 # increment count
else:
count_dict[item] = 1 # initialize count
print(count_dict)
Output: {'apple': 3, 'banana': 2, 'orange': 1}
Explanation: Dictionaries are perfect for counting, mapping, or storing relationships between data.
Step 5: PyCharm Project Layout
DataStructuresProject
├── lists_demo.py
├── tuples_demo.py
├── sets_demo.py
└── dicts_demo.py
# Example: lists_demo.py
fruits = ["Apple", "Banana", "Cherry", "Banana"]
# Remove duplicates using dict
unique_fruits = list(dict.fromkeys(fruits))
print(unique_fruits)
Step 6: Tips & Best Practices
- Use lists when order matters and data is mutable.
- Use tuples for fixed collections that shouldn’t change.
- Use sets to remove duplicates and perform set operations.
- Use dictionaries for counting, mapping keys to values efficiently.
- Combine data structures if needed, e.g., list of dictionaries.
- Use meaningful variable names to improve readability.
✔ End of Day 6 – You now understand Lists, Tuples, Sets, Dictionaries, removing duplicates, and can use them in projects efficiently!