Day 3 of My Python Journey: Exploring Dictionaries and Sets

·

3 min read

Hello, fellow Python learners! 🚀

Today, I dove into two fundamental data structures in Python: Dictionaries and Sets. Both are powerful and versatile, with unique features that make them essential tools for solving a variety of problems. Here’s a summary of what I learned!

Dictionary in Python

A dictionary in Python is like a real-world dictionary where you use a word (key) to find its meaning (value). It’s a collection of key-value pairs, and it allows for efficient lookups, updates, and deletions.

Points learned:

  • Dictionaries are used to store data values in Python.

    •   my_info = {
            "name" : "hello",
            "gender" : "female",
            "age" : 26,
            "is_adult" : True,
            "marks" : 60,
            "Courses" : ('C', 'Python')
        }
      
  • Dictionaries are mutable

  • Dictionaries are unordered and don't allow duplicate keys

  • Empty dictionary can be created and can be updated later.

    •   #empty/null dictionary
        null_dict = {}
      
        #Nested dictionary
        student = {
            "name" : "xyz",
            "gender" : "female",
            "courses" : {
                "chem" : 89,
                "math" : 78
            }
        }
        student.update({"city" : "hyd"})
      
  • Dictionaries can be type casted to lists.

  • Dictionaries can be created within lists and lists can be created within dictionaries

  • A few Dictionary methods

    myDict.keys() -> returns only keys (nested keys are not returned)

    myDict.values() -> returns all values

    myDict.items() -> returms all pairs as tuples

    myDict.get("key) -> returns value of that key

    myDict.update(new dict) -> can add new dict within a dict

    Eg: student.update({"city" : "hyd"})

Sets in Python

  • Set is a collection of unordered items.

  • Sets are immutable but elements of sets are immutable.

    • Set elements must be immutable: The things you put inside a set (its elements) must be unchangeable. This is because Python needs to keep track of each element using a special system (hashing), and mutable things like lists or dictionaries can break this system.

      Immutable elements: Numbers, strings, and tuples (with unchangeable items) are allowed.

      Mutable elements: Lists, dictionaries, or other sets are not allowed.

  • Eg of set: nums = {1, 2, 3, 4}

  • Set ignores duplicate values, but it won’t return an error

  • Eg: collection = {1, 2, 2,2, 4, 5,5,5, "hello", "hi", 5.6}

#empty set syntax
empty_set = set()
print(empty_set) #o/p: set()
empty_set.add(1)
print(empty_set) #o/p: {1}
#set.add(el) - adds an element in set
#set.remove(el) - removes an existing element if no element, it returns an error
#set.clear() - empties the set
#set.pop() - removes a random value
#set.union(another_set) - creates a new set(only unique elements) by combining 2 different sets
    #Eg: 
set1 = {1,2,3}
set2 = {3, 4,7} 
print(set1.union(set2)) #o/p: {1, 2, 3, 4, 7}
#set.intersection(another_set) - returns common elements of 2 sets - unique only
set3 = {1,2,3}
set4 = {3, 4,7} 
print(set3.intersection(set4)) #o/p: {3}