Skip to content
Worked Examples · Example 3.4

Q.#create a dictionary
dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
#getting value by specifying a key
print(dict1['Price(kg)'])
120

West Bengal WbchseTextbookSubjective· 2mImportance★★★★★est
16% · 4/25 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The code demonstrates dictionary creation and key-based value retrieval in Python; dict1['Price(kg)'] accesses the value 120 associated with the key 'Price(kg)'.

Understanding Dictionary Key Access

A Python dictionary stores data as key-value pairs, where each unique key maps to exactly one value. When you need to retrieve a specific piece of information, you use the key as an index — much like looking up a word in a real dictionary to find its definition.

The syntax dict1['Price(kg)'] tells Python: "Find the key 'Price(kg)' in dict1 and give me its associated value." This is direct access, not a search through all entries, which makes dictionaries extremely efficient for lookups.

Step-by-Step Execution

Creating the dictionary:

dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}

This initializes dict1 with three key-value pairs:

  • 'Fruit' → 'Apple'
  • 'Climate' → 'Cold'
  • 'Price(kg)' → 120

Notice that keys are strings (even 'Price(kg)' with special characters is valid), while values can be strings, numbers, or any Python object.

Printing the entire dictionary:

print(dict1)

Output:

{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}

The dictionary displays in the same curly-brace format, though the order shown depends on your Python version (3.7+ maintains insertion order).

Accessing a specific value:

print(dict1['Price(kg)'])

Output:

120

The square-bracket notation dict1['Price(kg)'] retrieves the integer 120. Python looks up the key 'Price(kg)' in the dictionary's internal hash table and returns the corresponding value.

Watch out

If you try to access a key that doesn't exist — say, dict1['Color'] — Python raises a KeyError. To avoid this, use dict1.get('Color') which returns None (or a default you specify) instead of crashing. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.