Fundamentals of Python :  5. Python Dictionaries

Fundamentals of Python

5. Python Dictionaries

Dictionaries are a core data structure in Python that store data in key-value pairs. They are incredibly efficient for data lookup, manipulation, and hierarchical data representation. Dictionaries are defined using curly braces { } and allow for rapid access, insertion, and deletion of values using unique keys.

Key Characteristics:

🧭 Ordered (from Python 3.7 onward): Maintains the order in which items are inserted.
🛠️ Mutable: Dictionary contents can be changed (add, update, or delete key-value pairs).
🔑 Unique Keys: Duplicate keys are not allowed; each key must be unique.
🧩 Nesting: Dictionaries can contain other dictionaries as values.
🔍 Lookup Efficiency: Ideal for key-based access and manipulation of data.

Dictionaries are widely used in data modeling, configuration files, JSON-like data structures, and storing structured information.

1. Create a Dictionary

  >>> student = {'name': 'Mary', 'id': '8776', 'major': 'CS'}
  >>> student
  Answer: {'name': 'Mary', 'id': '8776', 'major': 'CS'}

  >>> type(student)
  Answer: <type 'dict'>

📝 Explanation: A dictionary is created with keys such as 'name', 'id', and 'major'. Each key is mapped to a corresponding value.


2.  Get Length of Dictionary

  >>> len(student)
  Answer: 3

📝 Explanation: The len() function returns the number of key-value pairs in the dictionary.


3. Access a Value by Key

  >>> student['name']
 Answer: 'Mary'

📝 Explanation: Using the key 'name', we can retrieve its associated value.


 4. Add New Key-Value Pair

  >>> student['gender'] = 'female'
  >>> student
  Answer: {'name': 'Mary', 'id': '8776', 'major': 'CS', 'gender': 'female'}

📝 Explanation: We add a new key 'gender' to the dictionary with the value 'female'.


5. Get All Items (Key-Value Pairs)

  >>> student.items()
  Answer: [('name', 'Mary'), ('id', '8776'), ('major', 'CS'), ('gender', 'female')]

📝 Explanation: The items() method returns all key-value pairs as a list of tuples.


6. Get All Keys

  >>> student.keys()
 Answer: ['name', 'id', 'major', 'gender']

📝 Explanation: Returns a list of all keys in the dictionary.


7. Get All Values

  >>> student.values()
  Answer: ['Mary', '8776', 'CS', 'female']

📝 Explanation: Returns a list of all values stored in the dictionary.


8. Nested Dictionaries

  >>> student1 = {'name': 'David', 'id': '9876', 'major': 'ECE'}
  >>> students = {'1': student, '2': student1}
  >>> students
  Answer:
  {'1': {'name': 'Mary', 'id': '8776', 'major': 'CS', 'gender': 'female'},
  '2': {'name': 'David', 'id': '9876', 'major': 'ECE'}}

📝 Explanation: Dictionaries can contain other dictionaries, making them suitable for structured data models (e.g., student records).


9. Check if Dictionary Has a Key

  'name' in student
 Answer: True

  'grade' in student
  Answer: False

📝 Explanation:
The in operator checks whether a specific key exists in the dictionary.
👉 Note: has_key() was used in Python 2 and is deprecated in Python 3.


📢 If you found this guide useful, share it with fellow Python learners, follow for more coding tips, and leave a comment with any questions! 🐍💬

Comments

Popular posts from this blog

Fundamental of python : 1.Python Numbers