Make your code more efficient, use dictionary for mapping data

reza andriyunanto
Mar 30 · 2 min read

Hi everyone, are you ever meet this case? when we want to mapping data from one data to another data?

in case there any data user

{
"user_data": [{
"name": "Mukidi",
"id": "user-1"
}, {
"name": "Jepri",
"id": "user-2"
}, {
"name": "Sanjaya",
"id": "user-3"
}]
}

then there any data book order, for each user, in case we must get the data separately because not in one database, or maybe in different microservice

{
"book_order": [{
"name": "Clean Code",
"price": "25",
"user_id": "user-1"
}, {
"name": "Cracking the Coding Interview",
"price": "18",
"user_id": "user-1"
}, {
"name": "The Hundred-Page Machine Learning Book",
"price": "43",
"user_id": "user-2"
}, {
"name": "The Hundred-Page Machine Learning Book",
"price": "43",
"user_id": "user-3"
}, {
"name": "The Deep Learning Revolution",
"price": "34",
"user_id": "user-3"
}]
}

So, how we can merge that data to be one data sync? but wait, what like end data we want to achive? let assumed we need this output :

{
"list_user": [{
"name": "Mukidi",
"id": "user-1",
"book_order": [{
"name": "Clean Code",
"price": "25"
}, {
"name": "Cracking the Coding Interview",
"price": "18"
}]
}, {
"name": "Jepri",
"id": "user-2",
"book_order": [{
"name": "The Hundred-Page Machine Learning Book",
"price": "43"
}]
}, {
"name": "Sanjaya",
"id": "user-3",
"book_order": [{
"name": "The Hundred-Page Machine Learning Book",
"price": "43"
}, {
"name": "The Deep Learning Revolution",
"price": "34"
}]
}]
}

Are looping user_data, looping book_order, then check if user_id in each book_order is same in id in each user_data is solution? maybe, but there so many checking and make complexity of code to be n square because we traverse all the data. Like this :

for each_user in user_data:
for each_book in book_order:
if each_user[‘id’] == each_book[‘user_id’]:
append this book to this user

But, how if we create separate loop, first, we loop for mapping the book for each user_id, and user_id to be a key, like this, if in python, like this

book_mapping = {}
for each_book in book_order:
book_mapping.set_default(each_book[‘user_id’],[]).append(each_book)

and here the result of book_mapping :

{
"user-1": [{
"name": "Clean Code",
"price": "25",
"user_id": "user-1"
}, {
"name": "Cracking the Coding Interview",
"price": "18",
"user_id": "user-1"
}],
"user-2": [{
"name": "The Hundred-Page Machine Learning Book",
"price": "43",
"user_id": "user-2"
}],
"user-3": [{
"name": "The Hundred-Page Machine Learning Book",
"price": "43",
"user_id": "user-3"
}, {
"name": "The Deep Learning Revolution",
"price": "34",
"user_id": "user-3"
}]
}

the ideas is, we make id of parent to be a key of each book data

then what we must do?, to get the book is belonging to which user, we can access the `book_mapping` variable like this :

for each_user in user_data:
user_book = book_mapping[each_user[‘id’]]

so, we can reduce from complexity n square to be 2n is that great? or maybe any wrong concept? tell me in comment section.

Thank you!

Written by

Backend Engineer, Tech Enthusiasm, Love data and sometime curious about anything.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade