Home Coding Dictionary

Dictionary

0
Dictionary


# We start the week with a new DataStructure
# DICTIONARIES
# 
#  A dictionary consists of a collection of key-value pairs.
#  Like in real-world dictionaries, you have one part that helps you
# search into the dictionary and a second part that explains deeper
# what are you looking for.
# In Python dictionaries, each key-value pair maps the key to its associated value. 

# Let's create a dictionary
# to create a dictionary, you need to enclose a comma-separated list
# of key-value pairs in curly braces
# A colon (:) separates each key from its associated value

dict1 = {"a" : "value1", "b" : "value2", "c" : "value3", "d" : "value4", "e" : "value5"}

# copy www.AEC.codes
OUT = dict1
 
# NOTE
# In python 3.7 and newer dictionaries are ordered, it's
# mean that whatever you order the pairs in the dictionary,
# that order is maintained.
# Here in dynamo (with python 3.8, it seems that are not ordered



Dictionaries vs. lists

Lists and dictionaries have some common characteristics:

  1. are mutable;
  2. are dynamic;
  3. can be nested.

The primary difference between lists and dictionaries is:

  1. elements in a dictionary can be accessed by keys

1 – Dictionary are mutable
Mutable means a data type that allows entries, removed, and changed values at any time.

2 – Dictionaries are dynamic
Dictionaries can grow and shrink as needed, dynamically, during the process.

3 – Dictionary can be nested
A dictionary can contain another dictionary. A dictionary can also contain a list and vice versa.

4 – Elements in dictionaries can be accessed by keys
To access a value of a dictionary, you can access it by its key. In Lists, you have to access it with its index


Supported Methods

.clear() – Clears a dictionary
.get() – Returns the value for a key in the dictionary.
.items() – Returns a list of key-value pairs in a dictionary.
.keys() – Returns a list of keys in a dictionary.
.values() – Returns a list of values in a dictionary.
.pop() – Removes a key from a dictionary and returns its value.
.popitem() – Removes a key-value pair from a dictionary.
.update() – Merges a dictionary with another dictionary or with an iterable of key-value pairs.