+ 2
Can anyone please explain with context to use list vs. tuple vs. dictionary vs. set?
2 Antworten
+ 3
As we have seen in the previous lessons, Python supports the following data structures: lists, dictionaries, tuples, sets.
When to use a dictionary:
- When you need a logical association between a key:value pair.
- When you need fast lookup for your data, based on a custom key.
- When your data is being constantly modified. Remember, dictionaries are mutable.
When to use the other types:
- Use lists if you have a collection of data that does not need random access. Try to choose lists when you need a simple, iterable collection that is modified frequently.
- Use a set if you need uniqueness for the elements.
- Use tuples when your data cannot change.
Many times, a tuple is used in combination with a dictionary, for example, a tuple might represent a key, because it's immutable.
0
the basic difference between these are mutation of data. when data in dictionary is mutable, not in others. Lists can contain more than one type of data where as tuples can not.