Skip to main content

CT Week 8

Advanced Dictionary Concepts in Python

📚

Dictionary with List Values

A dictionary with list values is a dictionary where each key maps to a list of values. This is useful when you need to associate multiple values with a single key.

Examples of Dictionary with List Values

  • student_grades = {'Alice': [85, 90, 88], 'Bob': [78, 82, 85]}
  • shopping_list = {'fruits': ['apple', 'banana', 'cherry'], 'vegetables': ['carrot', 'broccoli']}
  • employee_projects = {'John': ['Project A', 'Project B'], 'Jane': ['Project C']}
  • class_schedule = {'Monday': ['Math', 'Science'], 'Tuesday': ['English', 'History']}
  • library = {'Fiction': ['1984', 'Brave New World'], 'Non-Fiction': ['Sapiens', 'Homo Deus']}

Dictionary Used to Find Common Elements

Dictionaries can be used to find common elements between different lists. By using the elements as keys and counting their occurrences, you can easily identify common elements.

Side Effects in Pseudocode for List in Dictionary

When working with lists in dictionaries, side effects can occur if you are not careful with how you modify the lists. Side effects refer to unintended changes to the data structure that can lead to bugs.

Example of Side Effects in Pseudocode

START
    DECLARE dictionary
    dictionary = {'key1': [1, 2], 'key2': [3, 4]}
    
    FUNCTION modify_list(dict, key, value)
        IF key IN dict THEN
            dict[key].APPEND(value)
        END IF
    END FUNCTION
    
    CALL modify_list(dictionary, 'key1', 5)
    PRINT dictionary  # Output: {'key1': [1, 2, 5], 'key2': [3, 4]}
    
    CALL modify_list(dictionary, 'key3', 6)
    PRINT dictionary  # Output: {'key1': [1, 2, 5], 'key2': [3, 4]}
END

In this example, the function modify_list modifies the list associated with a given key in the dictionary. If the key does not exist, no changes are made. This can lead to side effects if the function is called with keys that are not present in the dictionary.

PA:

GA:

Comments

Popular post

IITM Notes

Course Overview “These handwritten notes encompass topics in data science and civil services. The beauty of knowledge is that you don’t need to belong to any specific group; simply maintain your curiosity, and knowledge will find its way to you. I hope these notes are helpful. If they are, please consider leaving a comment below and follow my blog for updates.” Mathematics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Revision Statistics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11

Maths 1 week 1 Summary

Number System and Set Theory 📚 Number System and Set Theory This week, our teacher covered the basics of the number system. We were instructed to consider 0 as part of the natural numbers, as it will be treated as such in future subjects like Python. However, in exams, it will be explicitly stated whether 0 should be considered a natural number. The key topics from this week include set theory and the relationship between two sets. In set theory, we focused on three Venn diagram problems. In the context of relations, we discussed the concepts of reflexive, symmetric, transitive, and equivalence relations. Detailed Explanation 1.Union of Two Sets The union of two sets A and B is the set of elements that are in either A , B , or both. It is denoted as A ∪ B . 2.Intersection of Two Sets The intersection of two sets A and B is the set of elements that are in both A and B . It is denoted as A ∩ B . 3.Subt

Community page

Welcome To our IITM BS Students Community This community is a student commune where IIT Madras Bachelor of Science students are studying. Our community is managed by 15 community admins who oversee our WhatsApp community, Discord, and Telegram profiles. With more than 1000+ active members, we study together, share memes, watch movies, play games, and have fun. Our goal is to bring all online IITM students together to excel in exams while having fun. Community Admins Agampreet LinkedIn Ansh Ashwin Ambatwar Arti Dattu Dolly Elango Koushik Shrijanani Saksham Shivamani Shivam Instagram LinkedIn Join Our Community Subscribe to our YouTube page Join our meme team on