Saturday, March 23, 2019

Shyama Sankar Vellore: Python Dictionaries: Cheat Sheet

A cheat sheet for dictionaries in Python.
What is the dictionary datatype in Python? How to use it? Some key points and several examples of its usage.

Jump to the cheat sheet

Key points

  • Dictionary is a commonly used collection datatype in Python; the others being listssets, and tuples.
  • A dictionary is essentially a set of key-value pairs, where, the keys should be unique and immutable.
  • A dictionary is mutable, i.e., it can be modified in place after it is created. We can add, remove or update items in a dictionary.
  • Keys could be of any immutable data type, i.e., string, number, or even a tuple of immutable objects. [Note: Immutable objects are objects which cannot be modified in place once they are created. Python's built-in immutable objects include numbers, strings, and tuples].
  • Dictionaries are indexed using their keys.
  • Dictionaries are created using a pair of curly braces, i.e., {}, or by using the dict() constructor.
  • We can create and initialize a dictionary with items by adding a comma-separated list of key-value pairs within the curly braces. The 'dict' constructor also takes an optional iterable, mapping, or keyword arguments as input.
  • For examples on how to create a dictionary, add elements, update elements, remove elements, etc, see the cheat sheet.

Cheat sheet

Useful resources and references



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...