What is the difference between a python tuple and a dictionary?

PythonServer Side ProgrammingProgramming

They are very different data structures. Elements in a tuple have the following properties −

  • Order is maintained.

  • They are immutable

  • They can hold any type, and types can be mixed.

  • Elements are accessed via numeric (zero based) indices.

A Python dictionary is an implementation of a hash table. Elements of a dictionary have the following properties −

  • Ordering is not guaranteed

  • Every entry has a key and a value

  • Elements are accessed using key's values

  • Entries in a dictionary can be changed.

  • Key values can be of any hashable type (i.e. not a dict) and types can be mixed while values can be of any type (including other dict’s), and types can be mixed

Both of these data structures can be created using comprehensions. Examples

Tuple: (1, 'a', (3, 6, 8), 'string')
Dictionary: {'foo': [1, 2, 3], 'bar': 'baz'}
Published on 05-Jan-2018 12:41:07