Mastering List Operations in Python: Append, Remove, Sort

School
Tri-Village**We aren't endorsed by this school
Course
MATHAMATICS 11TH
Subject
Computer Science
Date
Dec 12, 2024
Pages
2
Uploaded by xunanglao02
print("Output #84: {}".format(a_list)) a_list.pop() a_list.pop() print("Output #85: {}".format(a_list)) This example shows how to add elements to and remove elements from a list. The append method adds single elements to the end of a list. You can use this method to create lists according to specific business rules. For example, to create a list of Cus- tomerX’s purchases, you could create an empty list called CustomerX, scan through a master list of all purchases, and when the program “sees” CustomerX in the master list, append the purchase value to the CustomerX list. The remove method removes a specific value from anywhere in a list. You can use this method to remove errors and typos from a list or to remove values from a list accord- ing to specific business rules. In this example, the remove method removes the num- ber 5 from a_list. The pop method removes single elements from the end of a list. Like with the remove method, you can use the pop method to remove errors and typos from the end of a list or to remove values from the end of a list according to specific business rules. In this example, the two calls to the pop method remove the numbers 6 and 4 from the end of a_list, respectively. reverse # Use reverse() to reverse a list in-place, meaning it changes the list # To reverse a list without changing the original list, make a copy first a_list.reverse() print("Output #86: {}".format(a_list)) a_list.reverse() print("Output #87: {}".format(a_list)) This example shows how to use the reverse function to reverse a list in-place. “In- place” means the reversal changes the original list to the new, reversed order. For example, the first call to the reverse function in this example changes a_list to [3, 2, 1]. The second call to the reverse function returns a_list to its original order. To use a reversed copy of a list without modifying the original list, first make a copy of the list and then reverse the copy. sort # Use sort() to sort a list in-place, meaning it changes the list # To sort a list without changing the original list, make a copy first unordered_list = [3, 5, 1, 7, 2, 8, 4, 9, 0, 6] print("Output #88: {}".format(unordered_list)) list_copy = unordered_list[:] 1ist_copy.sort() Python’s Basic Building Blocks | 29
Background image
print("Output #89: {}".format(list_copy)) print("Output #90: {}".format(unordered_list)) This example shows how to use the sort function to sort a list in-place. As with the reverse method, this in-place sort changes the original list to the new, sorted order. To use a sorted copy of a list without modifying the original list, first make a copy of the list and then sort the copy. sorted # Use sorted() to sort a collection of lists by a position in the lists my_lists = [[1,2,3,4], [4,3,2,1], [2,4,1,3]] v my_lists_sorted_by_index_3 = sorted(my_lists, key=lambda index_value:\ index_value[3]) print("Output #91: {}".format(my_lists_sorted_by_index_3)) This example shows how to use the sorted function in combination with a key func- tion to sort a collection of lists by the value in a specific index position in each list. The key function specifies the key to be used to sort the lists. In this case, the key is a lambda function that says, “Sort the lists by the values in index position three (i.e., the fourth values in the lists)” (We'll talk a bit more about lambda functions later.) After applying the sorted function with the fourth value in each list as the sort key, the sec- ond list [4,3,2,1] becomes the first list, the third list [2,4,1,3] becomes the second list, and the first list [1,2,3,4] becomes the third list. Also, note that whereas the sort function sorts the list in-place, changing the order of the original list, the sorted function returns a new sorted list and does not change the order of the original list. The next sorting example uses the standard operator module, which provides func- tionality for sorting lists, tuples, and dictionaries by multiple keys. To use the opera tor module’s itemgetter function in your script, add from operator import itemgetter at the top of your script: #! Jusr/bin/env python3 from import exp, log, sqrt import from datetime import date, time, datetime, timedelta from opera import itemgetter By importing the operator module’s itemgetter function, you can sort a collection of lists by multiple positions in each list: # Use itemgetter() to sort a collection of lists by two index positions my_lists = [[123,2,2,444], [22,6,6,444], [354,4,4,678], [236,5,5,678], \ [578,1,1,2908], [461,1,1,290]] my_lists_sorted_by_index_3_and_0 = sorted(my_lists, key=itemgetter(3,0)) print("Output #92: {}".format(my_lists_sorted_by index_3_and_0)) This example shows how to use the sorted() function in combination with the item getter function to sort a collection of lists by the values in multiple index positions 30 | Chapter 1: Python Basics
Background image