Python: Dictionary

By Xah Lee. Date: . Last updated: .

A python “dictionary” is a data structure. It is a unordered list of pairs, each is a key and a value. (It also known as “keyed list”, “associative array”, “hash table”, in other languages.)

Create Dictionary

{key1:val1, key2:val2, …}

# python 3

hh = {"john":3, "mary":4, "joe":5}
print(hh)
# {'joe': 5, 'john': 3, 'mary': 4}

Get Key's Value

dict[key]

# python 3
hh = {"john":3, "mary":4, "joe":5}
print(hh["mary"])
# 4

Add Entry

dict[key] = value

# python 3

hh = {"john":3, "mary":4}

# add a entry
hh["vicky"] = 99
print(hh)
# {'john': 3, 'mary': 4, 'vicky': 99}

Delete a Entry

del dict[key]

# python 3

hh = {"a":3, "b":4, "c":5}

# delete a entry
del hh["b"]
print(hh)
# {'a': 3, 'c': 5}

Get All Keys

dict.keys()

# python 3

hh = {"a":3, "b":4, "c":5}

print(hh.keys())
# dict_keys(['a', 'b', 'c'])

print(list(hh.keys()))
# ['a', 'b', 'c']

Get All Values

dict.values()

# python 3

hh = {"a":3, "b":4, "c":5}

# get all values
print(hh.values())
# dict_values([3, 4, 5])

Check Key Exist

key in dict

# python 3

hh = {"a":3, "b":4, "c":5}

# check if a key exists

print("b" in hh)
# True

What Type Can be Used as Key in Dictionary?

A key in dictionary can be anything that's hashable. Basically, string, tuple, number, or in general python data types that are “immutable”.

# python 3

# dict key can be anything that's hashable. typically, number, string, tuple. Most things that are “immutable”
hh = {2:3, "b":4, (3,4):5}

print(2 in hh)
# True

print((3,4) in hh)
# True

~2012 Thanks to 孙翰菲 for a tip.

Python List, Tuple, Dictionary

  1. List Basics
  2. Loop Thru List
  3. Map f to List
  4. Copy Nested List
  5. List Comprehension
  6. List Methods
  7. Sort
  8. Dictionary
  9. Loop Thru Dict
  10. Dict Methods
  11. Tuple
  12. Sets, Union, Intersection

If you have a question, put $5 at patreon and message me.

Python

  1. Python 3 Basics
  2. Python 2 Basics
  3. Python 2 and 3 Difference
  4. Print Version
  5. Builtin Help
  6. Quote String
  7. String Methods
  8. Format String
  9. Operators
  10. Complex Numbers
  11. True, False
  12. if then else
  13. Loop
  14. List Basics
  15. Loop Thru List
  16. Map f to List
  17. Copy Nested List
  18. List Comprehension
  19. List Methods
  20. Sort
  21. Dictionary
  22. Loop Thru Dict
  23. Dict Methods
  24. Tuple
  25. Sets
  26. Function
  27. Closure
  28. 2 Closure
  29. Decorator
  30. Class
  31. Object, ID, Type
  32. List Modules
  33. Write a Module
  34. Unicode 🐍

Regex

  1. Regex Basics
  2. Regex Reference

Text Processing

  1. Read/Write File
  2. Traverse Directory
  3. File Path
  4. Process Unicode
  5. Convert File Encoding
  6. Find Replace in dir
  7. Find Replace by Regex
  8. Count Word Frequency

Web

  1. Send Email
  2. GET Web Page
  3. Web Crawler
  4. HTTP POST

Misc

  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Append String in Loop
  7. Timing f timeit
  8. Keyword Arg Default Value Unstable
  9. Check Page Load Size
  10. Thumbnail Generation