When working with large amounts of data, there needs to be a method to the madness. Data Structures provide a way to organize this vast amount of data into an interpretable format. This tutorial will cover the following learning objectives:
What Lists Are and When to Use Them
What Tuples Are and When to Use Them
What Dictionaries Are and When to Use Them
What Sets Are and Whem to Use Them
What Lists Are and When to Use Them
Part I
Part II
Summary
A List is used to store multiple items in a single variable. To create a list, use the following syntax: list1 = [element1, element2, element3, element4]
Each item in the list is called an Element. To access a specific element in a list, use the element's index with the following syntax: list1[0]
If you want to replace a specific item in a list with a new value, use the following syntax: list1[0] = [new_value]
If you want to add a new element to the end of your list, use the Append method. This is used the following syntax: list1 = list1.append(element5)
If you want to remove an element by name, rather than by index, use the Remove method. This is used with the following syntax: list1 = list1.remove(element5)
If you want to remove the last element in a list, use the Pop method. This is used with the following syntax: list1 = list1.pop()
If you want to insert a new element at a specific index without removing the current element, use the Insert method. This is used with the following syntax: list1 = list1.insert([index_num], [element_name])
If you want to sort a list in ascending order, use the Sort method. This is used with the following syntax: list1 = list1.sort()
If you want to remove ALL elements in a list, use the Clear method. This is used with the following syntax: list1 = list1.clear()
NOTE: Notice that the index for the element's in a list starts at 0. This is a common computing principle and is a concept you should get used to.
What Tuples Are and When to Use Them
Summary:
A Tuple is a collection of values which is ordered and UNCHANGEABLE. These are used to group together related data. These are used with the following syntax: tuple1 = (element0, element1, element2)
If you want to count how many times a specific value occurrs in a tuple, use the following syntax: tuple1.count(item1)
If you want to find the index of a specific value in a tuple, use the following syntax: tuple1.index(item1)
NOTE: The concept of Tuples are commonly used in Relational Databases, where a tuple, represented by a row in a table, represents an observation (such as a specific product, order, or person).
What Sets Are and When to Use Them
Summary:
A Set is a collection of values that is unordered, unindexed, and contains no duplicate values. These are used with the following syntax: set1 = {element1, element2, element3}
If you want to add an element to a set, use the Add method. This is used with the following syntax: set1 = set1.add(element4)
If you want to remove an element from a set, use the Remove method. This is used with the following syntax: set1 = set1.remove(element4)
If you want to remove ALL elements from a set, use the Clear method. This is used with the following syntax: set1 = set1.clear()
If you want to add the elements from one set into another set, use the Update method. This is used with the following syntax: set1 = set1.update(set2)
If you want to combine the elements from two different sets into a new set, use the Union method. This is used with the following syntax: set3 = set1.union(set2)
If you want to see what one set has that another set doesn't, use the Difference method. This is used with the following syntax: set1.difference(set2)
If you want to see what one set has in common with another set, use the Intersection method. This is used with the following syntax: set1.intersection(set2)
NOTE: You should use a set rather than a list if you want to check if a specific value is in a range of values (whether strings or numeric values).
What Dictionaries Are and When to Use Them
Summary:
A Dictionary is a collection of key-value pairs that is ordered and changeable. No duplicate keys are allowed. These are used with the following syntax: dict1 = {key1: value1, key2: value2, key3: value3}
If you want to view all available methods for dictionaries, use the Dir function. This is used with the following syntax: dir(dict1)
If you want to get a description of all the methods available for dictionaries, use the Help function. This is used with the following syntax: help(dict1)
If you want to view the value associated with a specific key, use the Get method. This is used with the following syntax: dict1.get(key1)
If you want to add a key-value pair to the end of a dictionary, use the Update method. This is used with the following syntax: dict1 = dict1.update({key4: value4})
If you want to update the value in an existing key-value pair, use the Update method. This is used with the following syntax: dict1 = dict1.update({key2: value2})
If you want to remove a specific key-value pair from a dictionary, use the Pop method. This is used with the following syntax: dict1 = dict1.pop(key1)
If you want to remove the last key-value pair from a dictionary, use the Popitem method. This is used with the following syntax: dict1 = dict1.popitem()
If you want to remove ALL key-value pairs from a dictionary, use the Clear method. This is used with the following syntax: dict1 = dict1.clear()
If you want to view all the keys in a dictionary, use the Keys method. This is used with the following syntax: dict1.keys()
If you want to view all the values in a dictionary, use the Values method. This is used with the following syntax: dict1.values()
NOTE: You should use a set rather than a list if you want to check if a specific value is in a range of values (whether strings or numeric values).
Exercise
Congratulations! You just completed the Data Structures Tutorial! To help test your knowledge, let's
practice creating and manipulating various Data Structures.
**It's highly recommended that you
complete the exercise outlined in the previous tutorial before beginning this exercise.**
Instructions:
Open your IDE (e.g., VS Code, PyCharm, Spyder).
Create a New Python File. Name it "data-structures.py"
In the Newly Created Python File, complete the following tasks:
Declare a variable named "fruits" and give it the following values: "apple", "apple", "banana", "grape", "orange", "orange". Assume this variable allows duplicate elements. What type of Data Structure is this?
Remove the "grape" element from the "fruits" variable.
Declare a variable named "manager" and give it the following values: "Robert", "Senior Software Engineering Manager", 42, "Columbus, OH". Assume the values in this variable can't be changed. What type of Data Structure is this?
What is the index position of the "42" element in the "manager" variable?
Declare a variable named "national_brands" and give it the following values: "Nike", "Vans", "Converse", "New Balance". Assume this variable doesn't accept duplicate values. What type of Data Structure is this?
Declare a variable named "global_brands" and give it the following values: "Nike", "Vans", "Fila", "Ecco". Ensure this variable is a Set. What do the "local_brands" and "global_brands" variables have in common?
Declare a variable named "inventory" and give it the following values:
"Small T-Shirts": 45
"Medium T-Shirts": 72
"Large T-Shirts": 55
"XL T-Shirts": 60
What type of Data Structure is this?
Remove the key-value pair with the smallest value.
Exercise Completed! Click here to view the answers.
Have any issues with the above exercise? Post your question on Discord!