Jervis
Jervis

Reputation: 163

Can anyone summarize the noticeable difference between list, tuple, dictionary in Python?

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

Answers (2)

porgarmingduod
porgarmingduod

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

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74252

They are all covered in:

Python - Data Structures

  • List - when you have data that has some order
  • Tuple - when ordered data is to be immutable
  • Dictionary - when data is related by key - value pairs

Upvotes: 8

Related Questions