Reputation: 163
A little bit confusion about some data structure in Python.
Could any expert give some rules of thumb in order to help me get out of this mess?
Upvotes: 2
Views: 2528
Reputation: 7878
My take on the most important concepts regarding list/tuple/dict:
List - When you have a collection of items and may want to add/remove items, rearrange their order, and so on.
Tuple - When you have a collection of items and do NOT want to add/remove items, or rearrange their order. Realizing the usefulness of this comes with experience.
Dictionary - When you want to map certain keys to certain values, like a dictionary of words. The typical use case is when you have some kind of identifier (the key
) such as a person's name:
>>> addresses = {}
>>> addresses['john'] = 'somewhere' # Set john's address
>>> print "John's address is", addresses['john'] # Retrieve it
Upvotes: 4
Reputation: 74252
They are all covered in:
Upvotes: 8