What is a data structure?
A data structure is a way of storing and organizing data according to a certain format or structure, allowing us to access and manipulate the data in a certain way
Examples of data structures
tables to display schedules
a novel stores and organizes texts in paragraphs
Why are data structures important?
Data structures are a crucial part of computer programming as programming relies on our ability to store data, access that data, and manipulate that data to create the desired outcome.
Therefore, it is a top priority that engineers leverage data structures optimally to organize and manipulate data in an efficient and meaningful way
What are the 4 primary data structures in Python3?
So when thinking about a program… the data structure should be top of mind in the process of mapping out the path to success
what are the conditions needed to produce an optimal output from the given input?
What is a list?
a data structure that allows us to store elements of different data types in one container, linearly. Each element is indexed from 0 and up
Are lists mutable?
Yes, lists are mutable.
Are lists ordered?
Yes, lists are ordered linearly with each element at a specific index. The first element is at index 0, the next is at index 1, and so forth
What are two ways to merge two lists together?
merged_list = list_1 + list_2
merged_list = list_1.extend(list_2)
two ways of adding elements to a list
two ways of removing an element from a list
what is list slicing?
provides a sublist of the list being sliced.
the syntax for list slicing is:
list[starting_index, ending_exclusive_index, steps]
How can we verify the existence of an element in a list?
using the in operator
element_hello in list
this will return True if element_hello is in the list
and False if not in the list
What is list comprehension?
a technique that uses a for loop to create a new list from an existing list
since the result is always a NEW list, it’s good practice to assign list comprehension to a new variable
list comprehension is enclosed in square brackets
What is a tuple?
a tuple is similar to a list in that it can contain different data types and is ordered linearly and 0 indexed
a tuple is different because it is enclosed in parentheses AND is immutable. Meaning the tuple cannot be altered. BUT, the elements of a tuple are mutable and can be altered
since tuples are immutable, it is NOT possible to add or delete elements from an existing tuple
What is a dictionary?
an unordered data structure that stores data as key-value pairs, where each unique key is an index which holds the value associated with it
key:value pairs are accessed in a random, unordered manner
What is a set?
an unordered collection of data items
When would I use a set?
when you need to keep track of the existence of items
a set doesn’t allow duplicates. So you can convert another data structure to a set to remove duplicates
Set Theory Operations
are the operations of a set: union, intersection, difference
What is a Union?
a collection of all unique elements from both sets
syntax is the pipe, ‘|’, operator or the union() method
What is the intersection?
a collection of unique elements which are common between two sets
syntax is the ‘&’ operator or the intersection() method
What is the difference?
a collection of all the unique elements present in the first set and not present in the second set
syntax: - operator, or difference() method
Data Structure Conversion: Explicit Conversion
syntax: destination_structure_name(source_structure_object)
where destination structure is the data structure we want to convert to
and source_structure is the object we want to convert
Converting to a list
we use the listt() constructore
when converting a dictionary to a list, only the keys will be converted to a list