When should a dictionary be used?

Question

When should a dictionary be used?

Answer

Dictionaries are not always the best data structure to use in your program. They use up more memory than lists, so at scale it’s important to think about why you are choosing one over the other.
Lists are great for cases when you just need a list of items and possibly their index values.
If you’re making a dictionary where the key names are just 1, 2, 3, etc., consider using a list instead. Dictionaries are great when you need to map a relationship between keys and values where the keys need meaningful names other than index numbers.

3 Likes

I’ve got a question:

According to the syllabus, the .items, .keys, and .values methods “will not return the keys or values from the dictionary in any specific order.”

Yet, when using the methods one after another, they are all in the same order. And no matter how many times I run the script, it’s always the same order.

That seems to me like there actually is a specific order, just not one based on any logic. It’s not random.

So what exactly is meant by “the keys are not returned in any specific order”?

Here’s a previous reply that might help, basically you should avoid relying on the order if using earlier versions of python-

If I remember correctly from 3.6 cpython started retaining order (insertion order) and 3.7 made it an actual language requirement of Python. If you need compatibility then an OrderedDict could be useful.

2 Likes

What is the meaning of Tuples?

It’s normally related to the mathematical term:

Python has an implementation of a tuple as a built in data type: https://docs.python.org/3/library/stdtypes.html#tuples

They’ll likely be introduced in the course at some point or you can have a web search if you’re curious right now.