Example 1: Python New Disctionary
new_dict = dict()
#OR
new_dict = {}
Example 2: dictionary in python
# Dictionaries in Python are used to store set of data like Key: Value pair
# the syntax of a dictionary in Python is very simple we use {} inside that
# we define {Key: Value}, to separate multiple values we use','
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as expected.",
"Function": "A piece of code that you can easily call over and over again.",
"Loop": "The action of doing sommething again and again",
}
# to retrieve the values from a dictionary we use the Key name as an Index
# retrieving the Function's definition
print(programming_dictionary["Function"]) # this will print the definition of Function
# if you wanna print all the entries in the dictionary you can do that by for loop
for key in programming_dictionary:
print(programming_dictionary[key]) # prints all entries
# adding items to a dictionary
# the following code will add another entry to the dictionary called Variable
programming_dictionary["Variable"] = "The label to store some sort of data"
print(programming_dictionary["Variable"])
# editing the values of a key
# editing the value of variable
programming_dictionary["Variable"] = "Variables are nothing but reserved memory locations to store values. This means that when you create a variableyou reserve some space in memory"
# if you learnt something from this please upvote it
Example 3: python make a dictionary
#title : Dictionary Example
#author : Joyiscold
#date : 2020-02-01
#====================================================
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
#Assigning a value
thisdict["year"] = 2018
Example 4: python dictionary
#title :Dictionary Example
#author :Josh Cogburn
#date :20191127
#github :https://github.com/josh-cogburn
#====================================================
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
#Assigning a value
thisdict["year"] = 2018
Example 5: python dict
# decleration
my_dict = {
'spam': 'eggs',
'foo': 4,
100: 'bar',
2: 0.5
}
# access single values from the dictionary
print(my_dict['spam']) # eggs
print(my_dict['foo']) # 4
print(my_dict[100]) # bar
print(my_dict[2]) # 0.5
# iterate over the dictionary
for key, value in my_dict.items():
print(key, value)
# get length of the dictionary
print(len(my_dict)) # 4
# modify the dictionary
my_dict['baz'] = 'qux' # adds a pair
my_dict['baz'] = 'quxx' # also updates it
del my_dict['spam'] # removes a pair
# other methods
print(my_dict.copy()) # Returns a copy of the dictionary
print(my_dict.fromkeys('added', 100)) # Returns a dictionary with the specified keys and their values
print(my_dict.get('foo')) # Returns the value of the specified key
print(my_dict.items()) # Returns a list containing a tuple for each key value pair
print(my_dict.keys()) # Returns a list containing the dictionaries keys
print(my_dict.values()) # Returns a list of all the values in the dictionary
my_dict.setdefault('a', 'b') # Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
my_dict.pop('foo') # Removes the element with the specified key
my_dict.popitem() # Removes the last inserted key-value pair
my_dict.update({'baz': 'val'}) # Updates the dictionary with the specified key-value pairs
my_dict.clear() # Removes all the elements from the dictionary
Example 6: python3 add dictionary to dictionary
dict_1 = {"1":"a", "2":"b", "3":"c"}
dict_2 = {"4":"d", "5":"e", "6":"f"}
dict_1.update(dict_2)
print(dict_1)
#Output = {"1":"a", "2":"b", "3":"c", "4":"d", "5":"e", "6":"f"}