In lists, elements maintain their order unless they are explicitly commanded to re-order. These can be made up of any data type that can be all the same or mixed. However, elements in lists can only be accessed via numeric, zero-based indices.
In a dictionary, the order isn’t guaranteed. However, each entry will be assigned a key and a value. As a result, elements within a dictionary can be accessed by using their individual key.
So whenever you have a set of unique keys, you have to use a dictionary. Whenever a collection of items are in order, you can use a list.
It’s difficult to predict how an AI interview will unfold, so if they follow up by asking you how to get a list of all the keys in a dictionary, respond with the following:
To obtain a list of keys in a dictionary, you’ll have to use the following function keys():
mydict={‘a’:1,’b’:2,’c’:3,’e’:5}
mydict.keys()
dict_keys([‘a’, ‘b’, ‘c’, ‘e’])