[python] Python Data Structures (Dictionaries)

첫 영어 포스팅🙂 영어 공부도 할 겸 영어 공식 문서를 읽고 정리할 때는 영어로 작성해보려고 한다.

Dictionaries

  1. indexed by keys - keys can be any immutable type(can’t use lists)

    • e.g. string, number, tuple(only when it contain strings, numbers, or tuples)
  2. a set of key: value pairs
  3. called as “associative memories” or “associative arrays” in other languages.

How to use 😎

Creation & Insertion

{} : create an empty dictionary

>>> data = {'apple': 1000, 'banana': 2000}
>>> data['strawberry'] = 3000
>>> data
{'apple': 1000, 'banana': 2000, 'strawberry': 3000}

del dictionaryname[‘keyname’]

>>> del data['apple']
>>> data
{'banana': 2000, 'strawberry': 3000}

dict() constructor

  • build dictionaries directly from sequences of key-value pairs
>>> dict([('Jane', 25), ('Jisun', 26), ('Tom', 30)])
{'Jane': 25, 'Jisun': 26, 'Tom': 30}

# when the keys are simple strings
>>> dict(Jane=25, Tom=10, Jisun=17)
{'Jane': 25, 'Tom': 10, 'Jisun': 17}

dict comprehension

>>> {x: x**3 for x in (1, 2, 3)}
{1: 1, 2: 8, 3: 27}

list(dictionary_name)

  • return a list of keys used in the dictionary in insertion order
>>> list(data)
['banana', 'strawberry']

sorted(dictionary_name)

  • return a list of keys in the dictionary sorted
>>> data['apple'] = 5000
>>> list(data)
['banana', 'strawberry', 'apple']
>>> sorted(data)
['apple', 'banana', 'strawberry']

‘key’ in dictionary_name

  • return boolean value
>>> 'apple' in data
True
>>> 'lemon' in data
False

❗ Caution

  • Each key should be unique! if there are two keys with the same value, the new data will overwrite the previous data.

    >>> a = {1:'a', 1:'b'}
    >>> a
    {1: 'b'}

Source