## Python Data Structures Cheat Sheet <br/> <span style="color:grey; font-size: 24px;"><b>List</b></span> <table> <tr> <th>Package/Method</th> <th>Description</th> <th>Code Example</th> </tr> <tr> <td>append()</td> <td>The `append()` method is used to add an element to the end of a list.</td> <td>Syntax: list_name.append(element) Example: fruits = ["apple", "banana", "orange"] fruits.append("mango") print(fruits) </td> </tr> <tr> <td>copy()</td> <td>The `copy()` method is used to create a shallow copy of a list.</td> <td>Example 1: my_list = [1, 2, 3, 4, 5] new_list = my_list.copy() print(new_list) # Output: [1, 2, 3, 4, 5] </td> </tr> <tr> <td>count()</td> <td>The `count()` method is used to count the number of occurrences of a specific element in a list in Python.</td> <td>Example: my_list = [1, 2, 2, 3, 4, 2, 5, 2] count = my_list.count(2) print(count) # Output: 4 </td> </tr> <tr> <td>Creating a list</td> <td>A list is a built-in data type that represents an ordered and mutable collection of elements. Lists are enclosed in square brackets [] and elements are separated by commas.</td> <td>Example: fruits = ["apple", "banana", "orange", "mango"] </td> </tr> <tr> <td>del</td> <td>The `del` statement is used to remove an element from list. `del` statement removes the element at the specified index.</td> <td>Example: my_list = [10, 20, 30, 40, 50] del my_list[2] # Removes the element at index 2 print(my_list) # Output: [10, 20, 40, 50] </td> </tr> <tr> <td>extend()</td> <td>The `extend()` method is used to add multiple elements to a list. It takes an iterable (such as another list, tuple, or string) and appends each element of the iterable to the original list.</td> <td>Syntax: list_name.extend(iterable) Example: fruits = ["apple", "banana", "orange"] more_fruits = ["mango", "grape"] fruits.extend(more_fruits) print(fruits) </td> </tr> <tr> <td>Indexing</td> <td>Indexing in a list allows you to access individual elements by their position. In Python, indexing starts from 0 for the first element and goes up to `length_of_list - 1`.</td> <td>Example: my_list = [10, 20, 30, 40, 50] print(my_list[0]) # Output: 10 (accessing the first element) print(my_list[-1]) # Output: 50 (accessing the last element using negative indexing) </td> </tr> <tr> <td>insert()</td> <td>The `insert()` method is used to insert an element.</td> <td>Syntax: list_name.insert(index, element) Example: my_list = [1, 2, 3, 4, 5] my_list.insert(2, 6) print(my_list) </td> </tr> <tr> <td>Modifying a list</td> <td>You can use indexing to modify or assign new values to specific elements in the list.</td> <td>Example: my_list = [10, 20, 30, 40, 50] my_list[1] = 25 # Modifying the second element print(my_list) # Output: [10, 25, 30, 40, 50] </td> </tr> <tr> <td>pop()</td> <td>`pop()` method is another way to remove an element from a list in Python. It removes and returns the element at the specified index. If you don't provide an index to the `pop()` method, it will remove and return the last element of the list by default</td> <td>Example 1: my_list = [10, 20, 30, 40, 50] removed_element = my_list.pop(2) # Removes and returns the element at index 2 print(removed_element) # Output: 30 print(my_list) # Output: [10, 20, 40, 50] Example 2: my_list = [10, 20, 30, 40, 50] removed_element = my_list.pop() # Removes and returns the last element print(removed_element) # Output: 50 print(my_list) # Output: [10, 20, 30, 40] </td> </tr> <tr> <td>remove()</td> <td>To remove an element from a list. The `remove()` method removes the first occurrence of the specified value.</td> <td>Example: my_list = [10, 20, 30, 40, 50] my_list.remove(30) # Removes the element 30 print(my_list) # Output: [10, 20, 40, 50] </td> </tr> <tr> <td>reverse()</td> <td>The `reverse()` method is used to reverse the order of elements in a list</td> <td>Example 1: my_list = [1, 2, 3, 4, 5] my_list.reverse() print(my_list) # Output: [5, 4, 3, 2, 1] </td> </tr> <tr> <td>Slicing</td> <td>You can use slicing to access a range of elements from a list.</td> <td>Syntax: list_name[start:end:step] Example: my_list = [1, 2, 3, 4, 5] print(my_list[1:4]) # Output: [2, 3, 4] (elements from index 1 to 3) print(my_list[:3]) # Output: [1, 2, 3] (elements from the beginning up to index 2) print(my_list[2:]) # Output: [3, 4, 5] (elements from index 2 to the end) print(my_list[::2]) # Output: [1, 3, 5] (every second element) </td> </tr> <tr> <td>sort()</td> <td>The `sort()` method is used to sort the elements of a list in ascending order. If you want to sort the list in descending order, you can pass the `reverse=True` argument to the `sort()` method.</td> <td>Example 1: my_list = [5, 2, 8, 1, 9] my_list.sort() print(my_list) # Output: [1, 2, 5, 8, 9] Example 2: my_list = [5, 2, 8, 1, 9] my_list.sort(reverse=True) print(my_list) # Output: [9, 8, 5, 2, 1] </td> </tr> </table> <span style="color:grey; font-size: 24px;"><b>Tuple</b></span> <table> <tr> <th>Package/Method</th> <th>Description</th> <th>Code Example</th> </tr> <tr> <td>count()</td> <td>The count() method for a tuple is used to count how many times a specified element appears in the tuple. </td> <td>Syntax: tuple.count(value) Example: fruits = ("apple", "banana", "apple", "orange") print(fruits.count("apple")) #Counts the number of times apple is found in tuple. #Output: 2 </td> </tr> <tr> <td>index()</td> <td>The index() method in a tuple is used to find the first occurrence of a specified value and returns its position (index). If the value is not found, it raises a ValueError.</td> <td>Syntax: tuple.index(value) Example: fruits = ("apple", "banana", "orange") print(fruits[1]) #Returns the value at which apple is present. #Output: banana </td> </tr> <tr> <td>sum()</td> <td>The sum() function in Python can be used to calculate the sum of all elements in a tuple, provided that the elements are numeric (integers or floats).</td> <td>Syntax: sum(tuple) Example: numbers = (10, 20, 5, 30) print(sum(numbers)) #Output: 65 </td> </tr> <tr> <td>min() and max()</td> <td>Find the smallest (min()) or largest (max()) element in a tuple. </td> <td>Example: numbers = (10, 20, 5, 30) print(min(numbers)) #Output: 5 print(max(numbers)) #Output: 30 </td> </tr> <tr> <td>len()</td> <td>Get the number of elements in the tuple using len().</td> <td>Syntax: len(tuple) Example: fruits = ("apple", "banana", "orange") print(len(fruits)) #Returns length of the tuple. #Output: 3 </td> </tr> <tr> </tr> </table> <footer> <img align="left" src="https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/images/footer_6b.png" alt=""> </footer> <h3 align="center"> © IBM Corporation. All rights reserved. <h3/>