Problem Formulation and Solution Overview
π‘ Definition: Python Tuples are a type of Data Structure similar to Lists. However, Tuples are enclosed in round brackets () and are immutable, whereas Lists are enclosed in square brackets [] and are mutable. Another point worth mentioning is that Tuples tend to be more memory-efficient than Lists.
To make it more fun, we have the following running scenario:
Sample Data:
ncic_codes = [(101, 'Treason'), (102, 'Treason Misprision'),
(103, 'Espionage'), (104, 'Sabotage'),
(105, 'Sedition'), (106, 'Selective Service'),
(199, 'Sovereignty-Remarks'), (201, 'Military Desertion'),
(299, 'Military-Remarks'), (301, 'Illegal Entry')]Method 1: Use index to access Specific Tuple Element
This method uses the Tuple to access and display specific indexTuple elements from a List of Tuples.
code_103 = ncic_codes[2][1] print(code_103)
To access a specific Tuple element, reference the Tuple with the index value. In this example, the highlighted line retrieves the Offense Description for the matching Offense Code 103.
This is achieved by first referencing the specific Tuple element containing the desired information (ncic_codes[2]). If output to the terminal, the following Tuple displays.
| (103, ‘Espionage’) |
However, we need to append [1] (ncic_codes[2][1]) to retrieve the Offence Description. The results save to code_103 and output to the terminal.
| Espionage |
Method 2: Use List Comprehension to Access all Tuple Elements
This method uses List Comprehension to access and display all Tuple elements from a List of Tuples.
all_tuples = [x for x in ncic_codes] print(all_tuples)
Above uses List Comprehension to loop through each Tuple of ncic_codes and saves them to all_tuples. The contents of all_tuples is output to the terminal.
[(101, 'Treason'), (102, 'Treason Misprision'), (103, 'Espionage'), (104, 'Sabotage'), (105, 'Sedition'), (106, 'Selective Service'), (199, 'Sovereignty-Remarks'), (201, 'Military Desertion'), (299, 'Military-Remarks'), (301, 'Illegal Entry')] |
Method 3: Use List Comprehension to Search a List of Tuples
This method uses List Comprehension to search and return Tuples from a List of Tuples matching the stated criteria.
search_res = [x[0] for x in ncic_codes if x[0] >= 200] print(search_res)
Above uses List Comprehension to search for and return Tuples from a List of Tuples where the Offense Code is greater than or equal to (>=) 200. The results save to search_res and are output to the terminal.
| [201, 299, 301] |
Method 4: Use Unpacking
This method uses unpacking to extract all the data from a Tuple.
n_codes = (101, 102, 103, 104, 105) code_101, *_ = n_codes print(n_codes)
Above declares a Tuple containing the first five (5) Offense Codes from the National Crime Information Center and saves them to n_codes.
The following line references the first Tuple element, code_101. Then, *_ is called. This is the unpacking symbol and indicates to Python to retrieve (unpack) the remainder of the Tuple elements. These results are then output to the terminal.
| (101, 102, 103, 104, 105) |
Method 5: Use Slicing
This method uses slicing to retrieve specific Tuples from a List of Tuples.
sliced = ncic_codes[3:6] print(sliced)
Above uses slicing to retrieve three (3) Tuples from ncic_codes starting at position 3, and a stop position of 6 (6-1), equating to ncic_codes[3:6].
The results save to sliced and are output to the terminal as a sliced List of Tuples.
[(104, 'Sabotage'), (105, 'Sedition'), (106, 'Selective Service')] |
The Tuples below in bold indicate the extracted data using the above slicing range.
[(101, 'Treason'), (102, 'Treason Misprision'), (103, 'Espionage'), (104, 'Sabotage'), (105, 'Sedition'), (106, 'Selective Service'), (199, 'Sovereignty-Remarks'), (201, 'Military Desertion'), (299, 'Military-Remarks'), (301, 'Illegal Entry')] |
Method 6: Use filter and a Lambda
This method uses filter() and passes a lambda as an argument to search for and return Tuples from a List of Tuples matching the stated criteria.
ncic_filter = list(filter(lambda x: x[0] == 199, ncic_codes)) print(ncic_filter)
Above calls the filter() method and passes a lambda as an argument. This loops through ncic_codes and returns Tuples where the first element of the Tuple (the Offense Code) equals 199. The results are converted to a List format, saved to ncic_filter and output to the terminal.
[(199, 'Sovereignty-Remarks')] |
Summary
Programmer Humor – Blockchain
