Dictionaries are a type of collection in Python. If you recall, a list is integer indexes.
# Create the dictionary
Dict = {"key1": 1, "key2": "2", "key3": [3, 3, 3], "key4": (4, 4, 4), ('key5'): 5, (0, 1): 6}
Dict
These are like addresses. A list also has elements.
A dictionary has keys and values. The key is analogous to the index, they are like addresses but they don't have to be integers.
# Access to the value by the key
Dict["key1"]
# Access to the value by the key
Dict[(0, 1)]
They are usually characters. The values are similar to the element in a list and contain information.
To create a dictionary, we use curly brackets. The keys are the first elements. They must be immutable and unique. Each key is followed by a value separated by a colon.
# Create a sample dictionary
release_year_dict = {"Thriller": "1982", "Back in Black": "1980", \
"The Dark Side of the Moon": "1973", "The Bodyguard": "1992", \
"Bat Out of Hell": "1977", "Their Greatest Hits (1971-1975)": "1976", \
"Saturday Night Fever": "1977", "Rumours": "1977"}
release_year_dict
The values can be immutable, mutable, and duplicates. Each key and value pair is separated by a comma.
The key is used to look at the value. We use square brackets. The argument is the key. This outputs the value.
# Get value by keys
release_year_dict['Thriller']
# Get value by key
release_year_dict['The Bodyguard']
We can verify if an element is in the dictionary using the "in" command as follows. The command checks the keys. If they are in the dictionary, they return a true.
# Verify the key is in the dictionary
'The Bodyguard' in release_year_dict
If we try the same command with a key that is not in the dictionary, we get a false. If we try another key that is not in the dictionary, we get a false.
In order to see all the keys in the dictionary, we can use the method "keys" to get the keys. The output is a list like object with all the keys.
# Get all the keys in dictionary
release_year_dict.keys()
In the same way, we can obtain the values using the method values.
# Get all the values in dictionary
release_year_dict.values()
# Append value with key into dictionary
release_year_dict['Graduation'] = '2007'
release_year_dict
# Delete entries by key
del(release_year_dict['Thriller'])
del(release_year_dict['Graduation'])
release_year_dict
Sets : They are also a type of collection. This means that like lists and tuples, you can input different Python types. Unlike lists and tuples, they are unordered.
# Create a set
set1 = {"pop", "rock", "soul", "hard rock", "rock", "R&B", "rock", "disco"}
set1
This means sets do not record element position. Sets only have unique elements. This means there is only one of a particular element in a set. To define a set, you use curly brackets.
You place the elements of a set within the curly brackets. You notice there are duplicate items. When the actual set is created, duplicate items will not be present.
You can convert a list to a set by using the function "set", this is called type casting. You simply use the list as the input to the function "set". The result will be a list converted to a set.
# Convert list to set
album_list = [ "Michael Jackson", "Thriller", 1982, "00:42:19", \
"Pop, Rock, R&B", 46.0, 65, "30-Nov-82", None, 10.0]
album_set = set(album_list)
album_set
# Convert list to set
music_genres = set(["pop", "pop", "rock", "folk rock", "hard rock", "soul", \
"progressive rock", "soft rock", "R&B", "disco"])
music_genres
We input the list to the function set. The function set returns a set. But there are no duplicate elements.
# Sample set
A = set(["Thriller", "Back in Black", "AC/DC"])
A
We can add an item to a set using the add method. We just put the set name followed by a dot, then the add method. The argument is the new element of the set we would like to add, in this case, NSYNC.
# Add element to set
A.add("NSYNC")
A
The set A now has in NSYNC as an item. If we add the same item twice, nothing will happen as there can be no duplicates in a set.
# Try to add duplicate element to the set
A.add("NSYNC")
A
Let's say we would like to remove NSYNC from set A. We can also remove an item from a set using the remove method. We just put the set name followed by a dot, then the remove method. The argument is the element of the set we would like to remove, in this case, NSYNC. After the remove method is applied to the set, set A does not contain the item NSYNC.
# Remove the element from set
A.remove("NSYNC")
A
You can use this method for any item in the set.
We can verify if an element is in the set using the "in" command as follows. The command checks that the item, in this case AC/DC, is in the set. If the item is in the set, it returns true. If we look for an item that is not in the set we will get a false.
# Verify if the element is in the set
"AC/DC" in A
These are types of mathematical set operations. There are other operations we can do. There are lots of useful mathematical operations we can do between sets.
# Sample Sets
album_set1 = set(["Thriller", 'AC/DC', 'Back in Black'])
album_set2 = set([ "AC/DC", "Back in Black", "The Dark Side of the Moon"])
# Print two sets
album_set1, album_set2
The intersection of two sets is a new set containing elements which are in both of those sets.
In Python, we use an ampersand to find the union of two sets.
# Find the intersections
intersection = album_set1 & album_set2
intersection
# Use intersection method to find the intersection of album_list1 and album_list2
album_set1.intersection(album_set2)
After applying the intersection operation, all the items that are not in both sets disappear.
# Find the difference in set1 but not set2
album_set1.difference(album_set2)
album_set2.difference(album_set1)
The union of two sets is the new set of elements which contain all the items in both sets.
# Find the union of two sets
album_set1.union(album_set2)
We can check if a set is a subset using the issubset method.
# Check if superset
set(album_set1).issuperset(album_set2)
# Check if subset
set(album_set2).issubset(album_set1)
# Check if subset
set({"Back in Black", "AC/DC"}).issubset(album_set1)
# Check if superset
album_set1.issuperset({"Back in Black", "AC/DC"})