Make Sure You Choose The Right Data Structure // Python Tips

preview_player
Показать описание


🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

You should use the get method, so when the customer is not in the dictionary, you won't get an exception

ekrako
Автор

I had this exact same problem a few weeks ago! One thing that bothered me about this solution is that email is repeated twice, once in the dict and once in the class.

The email could be removed from the class but I think this solution is the easiest/clearest to use. Alternatively, a list comprehension call could be used to get the customer by email but it is probably slower to do that.

POINTS
Автор

Dict makes sense but we have emaile twice, one in the dictionary key and another on value, , is it ok or am I missing something here?

sanyk
Автор

Totally agree! It's a great hint for beginners. I'm always using this approach and trying to find where it could be useful in my code.
Currently have similar case but with a huge list of in-game items (like 9000+). Instead of going throug a list and check each element for its <item_id> it's a great solution just to have a dictionary like {<item_id>: PlayerItem(), ...}

HIMixoid
Автор

I would keep the list and freedom to search by whatever, even tho performance wise dictionaries are faster. plus, you might have to implement a unique check on the keys if emails are duplicated or ..it might become messy eventually. I handle several websocket connections at time, better the list

andreacazzaniga
Автор

What if we don't want to get the customer only by his email, but also by a customer_id or something ?
what is the appropriate way to implemente this ?

abdelghafourfid
Автор

Why exactly do you write CUSTOMERS in all uppercase? uppercase is reserved for constants and CUSTOMERS obviously is not a constant.

legion_prex
Автор

I had a problem like this, thing is there were two keys I needed to look by frequently (let's say, email and name for instance), so I couldn't just assign both of them as the key for the data, I had to search one of them by iterating through each element. Also, I frequently had to retrieve the value of one of them by searching the other. For instance, getting the name by looking for email, and viceversa. This is trivial if you iterate through each, but if you use one of those as the key, then it gets not hard, but clunky. I decided to use a list in the end. Would you have used another approach? (No, a database is overkill for the kind of data I'm managing).

tralphstreet
Автор

Don't you have to specify global keyword to modify global variable?

jjenn
Автор

Probably not important question, but: why bother giving a function name to what is already avaiable as a single dictionary key, i.e., why keep the `find_by_email` function?

MutleyXIII
Автор

I honestly thought you were pulling the piss by suggesting something so obvious, but looking at the responses, it seems like not everyone loves dicts as much as I do.

thesupercoach
Автор

A better solution would be to simply use a relational database

ApejanMusic
Автор

In line 5 I think you meant
CUSTOMERS[name] = ...
and not
CUSTOMERS[email] = ...

SpaghettDev