Dictionary Type in Python 

An associative data structure is a type of data structure where values are associated with specific keys, allowing efficient lookup and retrieval based on those keys. Unlike linear data structures that use indices to access elements, associative data structures use keys to uniquely identify and access values. 

  • One commonly used associative data structure in Python is the dictionary (dict) data type. 
  • A dictionary is a mutable, associative data structure of variable length. The syntax for declaring dictionaries in Python is given below. 

Creating a dictionary: To create a dictionary, you can use either the dict() constructor or the curly braces {} notation. 

# Using dict() constructor 
my_dict = dict() 
print(my_dict)  # Output: {}
# Using curly braces {} 
my_dict = {} 
print(my_dict)  # Output: {}

Adding key-value pairs to a dictionary: 

To add a key-value pair to a dictionary, you can assign a value to a new or existing key using the assignment operator (=). 

  • Dictionary daily_temps stores the average temperature for each day of the week.  
  • Each temperature has associated with it a unique key value (‘sun’, ‘mon’, etc.).  
  • Strings are often used as key values.

Accessing values in a dictionary:  

The syntax for accessing the values in a dictionary by referencing the associated key using square brackets []

daily_temps[‘sun’]  

Although strings are often used as key values, any immutable type may be used as well, such as a tuple (shown in Figure 9-3). 

 
In associative data structures like dictionaries, elements are stored and retrieved based on their key values using a process called hashing. Unlike indexed data structures, there is no concept of a specific order or position for elements. Hashing enables efficient storage and retrieval of elements by converting key values into index values. 

Dictionary Methods 

What is the difference between indexed data structure and Associative data structure? 

Indexed Data Structure: 

  • Indexed data structures store elements in a sequential manner and each element is assigned a unique index value. 
  • Elements in indexed data structures can be accessed using their index value, which represents their position in the structure. 
  • Examples of indexed data structures in Python include lists and tuples. 

# Indexed data structure – List 

fruits = ['apple', 'banana', 'orange'] 
fruits[0]  # Output: 'apple'

Associative Data Structure: 

  • Associative data structures store elements using key-value pairs, where each element has a unique key associated with it. 
  • Elements in associative data structures are accessed using their key values, rather than their position. 
  • Examples of associative data structures in Python include dictionaries. 
# Associative data structure - Dictionary 
fruit_prices 5 {'apples': .66, 'pears': .25, 'peaches': .74, 'bananas': .49} 
fruit_prices['apples'] # Output: 66
Design a site like this with WordPress.com
Get started