Net-informations.com
SiteMap  | About    

Difference between list and dictionary

List and dictionary are fundamentally different data structures . A list can store a sequence of objects in a certain order such that you can index into the list, or iterate over the list. Moreover, List is a mutable type meaning that lists can be modified after they have been created. Python dictionary is an implementation of a hash table and is a key-value store. It is not ordered and it requires that the keys are hashtable. Also, it is fast for lookups by key.

Elements in a list have the following characteristics:

  1. They maintain their ordering unless explicitly re-ordered (for example, by sorting the list).
  2. They can be of any type, and types can be mixed.
  3. They are accessed via numeric (zero based) indices.

Elements in a Dictionary have the following characteristics:

  1. Every entry has a key and a value
  2. Ordering is not guaranteed
  3. Elements are accessed using key values
  4. Key values can be of any hashtable type (i.e. not a dict) and types can be mixed
  5. Values can be of any type (including other dict’s), and types can be mixed
Usage:

Use a dictionary when you have a set of unique keys that map to values and to use a list if you have an ordered collection of items.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.