๐น Step 1: Create Dictionary
d = {'a': 1}Dictionary d has one key:
{'a': 1}๐น Step 2: Get Keys View
k = d.keys()d.keys() does NOT return a list
-
It returns a dictionary view object: dict_keys
-
This view is dynamic (live)
So k is linked to d, not a snapshot.
๐น Step 3: Modify Dictionary
d['b'] = 2Now dictionary becomes:
{'a': 1, 'b': 2}Because k is a live view, it automatically reflects this change.
๐น Step 4: Print Keys
print(list(k))k now sees both keys:
['a', 'b']✅ Final Output
['a', 'b']๐ฅ Key Takeaways (Important for Interviews)
dict.keys(), dict.values(), dict.items() are dynamic views
-
They update automatically when the dictionary changes
-
To freeze keys, use:
list(d.keys())


0 Comments:
Post a Comment