DEV Community

Cover image for Efficient Data Lookup Using Dictionaries in Data Structures
MABFLOW
MABFLOW

Posted on

Efficient Data Lookup Using Dictionaries in Data Structures

Working with structured data has become a common approach for developers. Building a structure helps organize the data and manipulate it in a simple way. Many developers rely on it while working with data structures such as Dictionary, List, Set, etc. The reason behind its popularity is the ability to provide a clear and structured view of the data instead of searching and exposing specific data each time you need it. In this post, we will delve more into performance, time complexity, and how we can use it to look up data.

In many occasions, searching for a specific item in data structures becomes doable with hardcoding the path to the specific data. This method is the easy approach to work with it. However, overusing it may lead to thousands of lines of code that always repeat the same process. This problem is common among developers, and in fact, developers aim to optimize the performance of the code. We are not going to discuss the consequences of having many lines etc., but instead, we will provide ways to solve it.

We have a users list:

users = [
        {"name": 'John', "age": 23, "city": 'NYC'},
        {"name": 'Alex', "age": 20, "city": 'Chic'},
        {"name": 'Mary', "age": 27, "city": 'London'},
]
Enter fullscreen mode Exit fullscreen mode

To access the user data, we should loop over the list first, then search each time for the user name to retrieve his data.

for user in users:
    if user['name'] == 'John':
           age = user['age']
           city = user['city']
Enter fullscreen mode Exit fullscreen mode

or using Comprehension:

age, city = [(user['age'], user['city']) for user in users if user['name'] == 'John'][0]
Enter fullscreen mode Exit fullscreen mode

This method is simpler for looking up data. However, imagine if the dataset contains hundreds of users. This can affect performance and slow down the program. To improve efficiency, we can optimize the time complexity so it runs fast.

Here where it comes the role of the structure. Instead of using the previous way to access to dataset. Building a structure of data is more reliable and efficient.

users_graph = {}

for user in users:
       users_graph[user['name']] = {
                   "age": user['age'], 
                   "city": user['city']
                    }
Enter fullscreen mode Exit fullscreen mode

Now we have built a structure that looks like:

users_graph = {
               "John": {"age": 23, "city": 'NYC'},
               "Alex": {"age": 20, "city": 'Chic'},
               "Mary": {"age": 27, "city": 'London'}
}
Enter fullscreen mode Exit fullscreen mode

To access user data:

john_age = users_graph.get('John').get('age')
john_city = users_graph.get('John').get('city')
Enter fullscreen mode Exit fullscreen mode

This simple method will reduce the time complexity of the program from O(n), to O(1).
Instead of looping over the whole dataset, it becomes easy to get directly to the user data through the structure.

In short, while working with small programs it is acceptable to use the normal method to access data. However, a real developer should consider improving the performance and time complexity of the program which can boost the speed and efficiency.

Top comments (2)

Collapse
 
samirkng profile image
samir elboukaoui

Great work, keep it up

Collapse
 
mabflow profile image
MABFLOW

! appreciate it 💥