Python hash tables

The Python dictionary variable type is similar to the hash (associative) array of other languages. Python dictionary can store different variable types. dictionary initialization:

>>> x = {"USA":3,"China":13,"Brazil":1}
>>> x
{'Brazil':1,'China':13,'USA':3}
>>> x = {2:3,"China":13,"Brazil":[1,2,3]}
>>> x
{2: 3, 'Brazil': [1,2,3],'China': 13}
Add items to dictionary:
>>> x[12] = "aa"
>>> x
{2: 3, 12: 'aa', 'Brazil': [1,2,3],'China': 13}
Delete items of dictionary:
>>> del x[2]
>>> x
{12: 'aa', 'Brazil': [1,2,3],'China': 13}
>>> x.clear() #remove all entries in x
>>> del x delete x

Update dictionary item value:
>>> x = {12: 'aa', 'Brazil': [1,2,3],'China': 13}
>>> x[12] = "bb" #update x[12] value
>>> x
{12: 'bb', 'Brazil': [1,2,3], 'China': 13}














endmemo.com © 2024  | Terms of Use | Privacy | Home