What are Data Structures in Python?
Data structures are the fundamental constructs around which you build your programs. Let's look at the 4 most common types, List
,Tuple
,Set
and Dictionary
.
We can compare these structure on the following five characteristics:
- Mutable: We can change, add , and remove items in the structure after it has been created.
- Ordered: The items in the structure have a defined order that will not change. If you add new items they will be placed at the end of the structure.
- Duplicates: Allows items with the same value.
- Different Objects: Allows objects of different data types.

When Should I use a Dictionary as a Data Structure?
As we seen from before a Dictionary
is a structure that stores key-value pairs. We would use this structure in the following scenarios:
- Quick access to a data point (value), since the data points are uniquely associated with labels (key)
- When the order of the data points is irrelevant.
How is Memory Stored and Retrieved in a Python Dictionary?
Python's Dictionary
is implemented using hash tables. This table is made up of three parts:
- Hash ID
- Keys
- Values
Because of this concept, Python's dictionary is a useful data structure for quickly retrieving relevant value by calling the key. Let's consider the following Python dictionary example where the key is the piece of clothing, and the value is the price:
1clothes_price = {'dress':29.99, 'shoes':19.99, 'shirt': 19.99, 'shorts':14.99}
2
3clothes_price['dress']
By using the square bracket, we can retrieve the value of a specific key giving the following output:
129.99
When Sorting a List What is the Difference Between sorted()
and .sort()
?
To see the difference, let's illustrate with an example.
1# Our list
2my_list = [2,9,1,6,2,5]
sorted
1sort_method_1 = sorted(my_list)
2print('Sorted List 1: ', sort_method_1, 'Old List: ', my_list)
We get the following output:
1Sorted List: [1, 2, 2, 5, 6, 9] Old List: [2, 9, 1, 6, 2, 5]
.sort()
1sort_method_2 = sort_example.sort()
2print('Sorted List 2: ', sort_method_2, 'Old List: ', my_list)
We get the following output:
1Sorted List 2: None Old List: [1, 2, 2, 5, 6, 9]
As demonstrated above sorted()
creates another list that is sorted whereas .sort()
changes our original list my_list instead of creating another.