This is the second module (Python Data Structures) for Python for Data Science, a course by IBM's Cognitive Class Labs.
OUTLINE:
- Lists and Tuples
- Sets
- Dictionaries
Lists and Tuples
Lists and tuples are compound data types
1. TUPLES
Ordered sequence
Separated by commas
Within a set of (parentheses)
![](https://static.wixstatic.com/media/49162d_a81fdb38d80e430daa2c35bf3994d68c~mv2.png/v1/fill/w_257,h_108,al_c,q_85,enc_auto/49162d_a81fdb38d80e430daa2c35bf3994d68c~mv2.png)
Each element of a tuple can be accessed via an index or a negative index
![](https://static.wixstatic.com/media/49162d_2d4d80301f80480fa01beb960b6ac0ff~mv2.png/v1/fill/w_218,h_184,al_c,q_85,enc_auto/49162d_2d4d80301f80480fa01beb960b6ac0ff~mv2.png)
Concatenate
Combine tuples by adding them, resulting in an index
![](https://static.wixstatic.com/media/49162d_6497088bd45a4a8d8ea60df55657ace6~mv2.png/v1/fill/w_311,h_105,al_c,q_85,enc_auto/49162d_6497088bd45a4a8d8ea60df55657ace6~mv2.png)
We can slice the tuple
![](https://static.wixstatic.com/media/49162d_af3d1101f5e7493b9584d7ada18c9ca7~mv2.png/v1/fill/w_256,h_143,al_c,q_85,enc_auto/49162d_af3d1101f5e7493b9584d7ada18c9ca7~mv2.png)
len
Obtain the length of a tuple
![](https://static.wixstatic.com/media/49162d_ae952f8c7f3546d0aa34bb40d5f390f0~mv2.png/v1/fill/w_342,h_60,al_c,q_85,enc_auto/49162d_ae952f8c7f3546d0aa34bb40d5f390f0~mv2.png)
Tuples are immutable, meaning we can’t change them
Each variable references the same immutable tuple object
If we wanted to change the element at index 2, we couldn’t because the tuple is immutable
If we would like to manipulate a tuple, we must create a new tuple entirely
![](https://static.wixstatic.com/media/49162d_2902f62e52fd4dc49d6c8bd0ed19c710~mv2.png/v1/fill/w_322,h_408,al_c,q_85,enc_auto/49162d_2902f62e52fd4dc49d6c8bd0ed19c710~mv2.png)
sorted
Sorts the tuple
![](https://static.wixstatic.com/media/49162d_605cfd1a2b25418d9cc8b8cef6acd19e~mv2.png/v1/fill/w_317,h_104,al_c,q_85,enc_auto/49162d_605cfd1a2b25418d9cc8b8cef6acd19e~mv2.png)
nesting
A tuple can contain other tuples as well as other complex data types
Elements can be accessed using the standard indexing methods
Visualize it as a tree
Access deeper levels of the tree by adding another square bracket
![](https://static.wixstatic.com/media/49162d_8e3d56e32ccd447f97158dc0f9337235~mv2.png/v1/fill/w_387,h_326,al_c,q_85,enc_auto/49162d_8e3d56e32ccd447f97158dc0f9337235~mv2.png)
2. LISTS
Ordered sequence
Separated by commas
Lists are represented with [square brackets]
Can contain strings, floats, integers, and nest other lists or tuples
The same indexing conventions apply to lists (including a negative index that starts at -1, rather than 0)
![](https://static.wixstatic.com/media/49162d_df090a38ca42412f975f8b6789ad93a4~mv2.png/v1/fill/w_453,h_102,al_c,q_85,enc_auto/49162d_df090a38ca42412f975f8b6789ad93a4~mv2.png)
Slicing
We can slice the list to get the specified elements
![](https://static.wixstatic.com/media/49162d_cfa0efdccef84e52836031fc2b4c9f35~mv2.png/v1/fill/w_208,h_68,al_c,q_85,enc_auto/49162d_cfa0efdccef84e52836031fc2b4c9f35~mv2.png)
Concatenate
Combine lists by adding them
![](https://static.wixstatic.com/media/49162d_02a253f669504b9d86d14576889bc9ab~mv2.png/v1/fill/w_456,h_104,al_c,q_85,enc_auto/49162d_02a253f669504b9d86d14576889bc9ab~mv2.png)
Mutable
Big difference between lists and tuples is that lists can be changed
![](https://static.wixstatic.com/media/49162d_0252927a64704bb297871d957e25763a~mv2.png/v1/fill/w_307,h_159,al_c,q_85,enc_auto/49162d_0252927a64704bb297871d957e25763a~mv2.png)
.extend
This is a “method”
Concatenates a new list to the original list
Modifies the list by adding to it
![](https://static.wixstatic.com/media/49162d_eb39337132244d13a68360c418e65376~mv2.png/v1/fill/w_483,h_102,al_c,q_85,enc_auto/49162d_eb39337132244d13a68360c418e65376~mv2.png)
.append
Adds only one element to the list
![](https://static.wixstatic.com/media/49162d_c7e9128e6efb472ea64b032717f12e6a~mv2.png/v1/fill/w_499,h_111,al_c,q_85,enc_auto/49162d_c7e9128e6efb472ea64b032717f12e6a~mv2.png)
del
Delete an element of the list
![](https://static.wixstatic.com/media/49162d_2268220562bc4e688304cfea5890a589~mv2.png/v1/fill/w_295,h_184,al_c,q_85,enc_auto/49162d_2268220562bc4e688304cfea5890a589~mv2.png)
.split
Convert a string to a list using split
Method split converts every group of characters separated by a space into an element of a list
Can use split function to separate strings on a specific character known as a delimiter
![](https://static.wixstatic.com/media/49162d_783995c4fa054278bff8d1ac294aa3d3~mv2.png/v1/fill/w_322,h_158,al_c,q_85,enc_auto/49162d_783995c4fa054278bff8d1ac294aa3d3~mv2.png)
Aliasing
Multiple names refer to the same object
![](https://static.wixstatic.com/media/49162d_f46499d5ba59454583318b1beca5a31e~mv2.png/v1/fill/w_331,h_220,al_c,q_85,enc_auto/49162d_f46499d5ba59454583318b1beca5a31e~mv2.png)
Changing an element in one list when both lists reference the same values will affect both lists
![](https://static.wixstatic.com/media/49162d_b026965685e04691bec4cefd07cd5b7e~mv2.png/v1/fill/w_320,h_179,al_c,q_85,enc_auto/49162d_b026965685e04691bec4cefd07cd5b7e~mv2.png)
Clone
Duplicate the list without having to reference the same values in the new list
![](https://static.wixstatic.com/media/49162d_f775ee9f9abb4e52b513e91b4c26505b~mv2.png/v1/fill/w_322,h_404,al_c,q_85,enc_auto/49162d_f775ee9f9abb4e52b513e91b4c26505b~mv2.png)
Sets
Sets
A type of collection
Can input different python types
Unordered
do not record element position
Only have unique elements
Define sets
Use curly brackets
Place elements of set within curly brackets
Duplicated items will not show
![](https://static.wixstatic.com/media/49162d_0d5555d9d858447eb096e341d1fdc2a0~mv2.png/v1/fill/w_548,h_87,al_c,q_85,enc_auto/49162d_0d5555d9d858447eb096e341d1fdc2a0~mv2.png)
Type-casting
Convert list to a set using the function set
Use the list as the input to the function set
Result: list converted to a set
![](https://static.wixstatic.com/media/49162d_79bd4843ea544612af2d67de4e0ffce6~mv2.png/v1/fill/w_358,h_105,al_c,q_85,enc_auto/49162d_79bd4843ea544612af2d67de4e0ffce6~mv2.png)
Set Operations
Can be used to change the set
.add
Add an item to a set
![](https://static.wixstatic.com/media/49162d_7efc4ff7291242ac943e3deea1705cfe~mv2.png/v1/fill/w_559,h_100,al_c,q_85,enc_auto/49162d_7efc4ff7291242ac943e3deea1705cfe~mv2.png)
.remove
Remove an item from a set
![](https://static.wixstatic.com/media/49162d_e3dc7bbd5c3a448ab3ed059b9846f98d~mv2.png/v1/fill/w_426,h_88,al_c,q_85,enc_auto/49162d_e3dc7bbd5c3a448ab3ed059b9846f98d~mv2.png)
In
Verify if an item is in the set
![](https://static.wixstatic.com/media/49162d_f46c210a68124aac82375def2cfdbddf~mv2.png/v1/fill/w_174,h_70,al_c,q_85,enc_auto/49162d_f46c210a68124aac82375def2cfdbddf~mv2.png)
Mathematical operations between sets
Intersection
&
Causes all items that are not in both sets to disappear
![](https://static.wixstatic.com/media/49162d_3f375136ff50488a838b9ef08926d70c~mv2.png/v1/fill/w_517,h_133,al_c,q_85,enc_auto/49162d_3f375136ff50488a838b9ef08926d70c~mv2.png)
.union
New set of elements which contains all items in both sets
![](https://static.wixstatic.com/media/49162d_6292816f98924db3b7f6232956bd849b~mv2.png/v1/fill/w_598,h_67,al_c,q_85,enc_auto/49162d_6292816f98924db3b7f6232956bd849b~mv2.png)
.issubset
Check if a set is a subset
![](https://static.wixstatic.com/media/49162d_2516c27818594fad8375eaa7079d3134~mv2.png/v1/fill/w_303,h_65,al_c,q_85,enc_auto/49162d_2516c27818594fad8375eaa7079d3134~mv2.png)
Dictionaries
Dictionaries are a type of collection
A dictionary has keys and values
![](https://static.wixstatic.com/media/49162d_d93bffc8737b44138bbbd5eb1f4a1e50~mv2.png/v1/fill/w_547,h_414,al_c,q_85,enc_auto/49162d_d93bffc8737b44138bbbd5eb1f4a1e50~mv2.png)
Dictionaries
Denoted with curly brackets
The keys must be immutable and unique
Each key is followed by a value separated by a colon
Can be immutable, mutable, and duplicates
Each key and value pair is separated by a comma
![](https://static.wixstatic.com/media/49162d_e7dffd7c6bd840fbacebc303593dc4cb~mv2.png/v1/fill/w_472,h_41,al_c,q_85,enc_auto/49162d_e7dffd7c6bd840fbacebc303593dc4cb~mv2.png)
Assign the dictionary to a variable
Use the key to look up a value
Use square brackets
Argument is the key, which outputs the value
![](https://static.wixstatic.com/media/49162d_a97bfa4e1bcb44bb918e2b46ca1c7721~mv2.png/v1/fill/w_577,h_78,al_c,q_85,enc_auto/49162d_a97bfa4e1bcb44bb918e2b46ca1c7721~mv2.png)
Add a new entry
![](https://static.wixstatic.com/media/49162d_fd4df36f20e5443ca7845dbd7b4dfa24~mv2.png/v1/fill/w_248,h_137,al_c,q_85,enc_auto/49162d_fd4df36f20e5443ca7845dbd7b4dfa24~mv2.png)
Delete an entry
![](https://static.wixstatic.com/media/49162d_504a04aea1d04026997d7db6c737c3ad~mv2.png/v1/fill/w_370,h_99,al_c,q_85,enc_auto/49162d_504a04aea1d04026997d7db6c737c3ad~mv2.png)
Verify if an element is in the dictionary
![](https://static.wixstatic.com/media/49162d_8d722f6d43864ceda9700fbde091ced8~mv2.png/v1/fill/w_202,h_76,al_c,q_85,enc_auto/49162d_8d722f6d43864ceda9700fbde091ced8~mv2.png)
.keys
Used to get a list of the keys
![](https://static.wixstatic.com/media/49162d_d08df8a42ace4f099858cbdd0744ffc0~mv2.png/v1/fill/w_507,h_66,al_c,q_85,enc_auto/49162d_d08df8a42ace4f099858cbdd0744ffc0~mv2.png)
Comments