Getting Started with Python: Dictionaries, Tuples and Sets
Python Data types that are use for collections
Just before we move on, if you need to go through the basics of data types and variables, kindly check the previous post here: https://medium.com/dsn-ai-futa/variables-and-data-types-c427233e684b
DICTIONARIES
A dictionary is an associative container that contains the items in key/value pairs. The values of key-item data structure (unlike lists and strings that can be accessed using indexing) can be accessed using the keys. The keys are assigned to the values in the data structure and used to save them to memory. The choice of deciding between sequences like a list and mappings like a dictionary often depends on the specific situation. As you become a stronger programmer, choosing the right storage format will become more intuitive.
Now let’s cover the basics of dictionaries!
Creating a Dictionary
A Python dictionary can be created in various ways. Some of them are listed below;
i. Creating Empty Dictionary
ii. Creating Dictionaries with Literals: We can create a dictionary by passing key-value pairs literals
iii. Creating Dictionaries by passing parameters in dict constructor
iv. Creating Dictionaries by a list of tuples
v. Creating a Dictionary by two lists
After creating our dictionaries, we can also add more keys and value pairs to the dictionary
>>> d[‘new_key’] = ‘new item’
>>> print(d)
Output: {‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘new_key’: ‘new item’}
Note: Dictionaries are unordered! This may not be clear at first with smaller dictionaries, but as dictionaries get larger they won’t retain order, which means they can not be sorted! If you need order and the ability to sort, stick with a sequence, such as a List!
Dictionaries, just like lists, are very flexible in the data types they can hold, they can hold numbers, strings, lists, and even other dictionaries!
Keep dictionaries in mind when you need to create a mapping and don’t care about the order of the elements. For example;
>>> short_names = {“AAU”: “Ambrose Alli University”,
“OAU”: “Obafemi Awolowo University”,
“UNILAG”: “University of Lagos”,
“NDA”: “National Defence Academy”}
>>> short_names[“AAU”]
Output: ‘Ambrose Alli University’
Dictionary methods
After creating your dictionaries, there might be some operations you want to perform on them which can be done with the inbuilt Python dictionary methods.
. values ( ) to return the values of the dictionary
. keys ( ) to return the keys
. items ( ) to return tje items i.e. both the key and value pairs
clear(): Removes all the elements from the dictionary
copy(): Returns a copy of the dictionary
fromkeys(): Returns a dictionary with the specified keys and value
get(): Returns the value of the specified key
pop(): Removes the element with the specified key
popitem(): Removes the last inserted key-value pair
setdefault(): Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update(): Updates the dictionary with the specified key-value pairs
TUPLES
Tuples are ordered sequences just like a list, but have one major difference, they are immutable, which means that you can not change them. So in practice what does this actually mean? It means that you can not reassign an item once its in the tuple, unlike a list, where you can do a reassignment. Unlike lists and dictionaries, tuples have only two methods. The .index( ) method which is used to return the position an element occurs in a tuple and the .count( ) which is used to count the number of occurrence an element appears in a tuple.
Why use tuples?
Lists and tuples are very similar, so you may find yourself exchanging use cases for either one. However, you should use a tuple for collections or sequences that shouldn’t be changed, such as the dates of the year, or user information such as an address,street, city , etc.
SETS
Another fundamental Data Structure is The Set!
Sets are unordered collection of unique elements. We can construct them by using the set() function. Let’s go ahead and make a set to see how it works:
A set has only unique entries. So what happens when we try to add something that is already in a set?
>>> x.add(1) #adding elements to a set
>>> print(x)
Output: {1, 2}
We can cast a list with multiple repeated elements to a set so as to get the unique elements. For example:
Thanks for reading!