11.8 Copy Lists

Copy a List You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. There are ways to make a copy, one way is to use the built-in List method copy(). Example Make a copy of a list with the copy() method:thislist = [“apple”, “banana”, “cherry”]mylist = thislist.copy()print(mylist) Output:[‘apple’,… Read More »

11.7 Sort Lists

Sort List Alphanumerically List objects have a sort() method that will sort the list alphanumerically, ascending, by default: Example Sort the list alphabetically:thislist = [“orange”, “mango”, “kiwi”, “pineapple”, “banana”]thislist.sort()print(thislist) Output:[‘banana’, ‘kiwi’, ‘mango’, ‘orange’, ‘pineapple’] Example Sort the list numerically:thislist = [100, 50, 65, 82, 23]thislist.sort()print(thislist) Output:[23, 50, 65, 82, 100] Sort Descending To sort descending, use the keyword argument reverse = True: Example Sort the list… Read More »

11.6 List Comprehension

List Comprehension List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name. Without list comprehension you will have to write… Read More »

11.5 Loop Lists

Loop Through a List You can loop through the list items by using a for loop: Example Print all items in the list, one by one:thislist = [“apple”, “banana”, “cherry”]for x in thislist:  print(x) Output:applebananacherry Loop Through the Index Numbers You can also loop through the list items by referring to their index number. Use the range() and len() functions to create a suitable iterable. Example Print… Read More »

11.4 Remove List Items

Remove Specified Item The remove() method removes the specified item. Example Remove “banana”:thislist = [“apple”, “banana”, “cherry”]thislist.remove(“banana”)print(thislist) Output:[‘apple’, ‘cherry’] Remove Specified Index The pop() method removes the specified index. Example Remove the second item:thislist = [“apple”, “banana”, “cherry”]thislist.pop(1)print(thislist) Output:[‘apple’, ‘cherry’] If you do not specify the index, the pop() method removes the last item. Example Remove the last item:thislist = [“apple”, “banana”, “cherry”]thislist.pop()print(thislist) Output:[‘apple’, ‘banana’] The del keyword also… Read More »

11.3 Add List Items

Append Items To add an item to the end of the list, use the append() method: Example Using the append() method to append an item:thislist = [“apple”, “banana”, “cherry”]thislist.append(“orange”)print(thislist) Output:[‘apple’, ‘banana’, ‘cherry’, ‘orange’] Insert Items To insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index: Example Insert an item as the second… Read More »

11.2 Change List Items

Change Item Value To change the value of a specific item, refer to the index number: Example Change the second item:thislist = [“apple”, “banana”, “cherry”]thislist[1] = “blackcurrant”print(thislist) Output:[‘apple’, ‘blackcurrant’, ‘cherry’] Change a Range of Item Values To change the value of items within a specific range, define a list with the new values, and refer to the range… Read More »

11.1 Access List Items

Access Items List items are indexed and you can access them by referring to the index number: Example Print the second item of the list:thislist = [“apple”, “banana”, “cherry”]print(thislist[1]) Output:banana Note: The first item has index 0. Negative Indexing Negative indexing means start from the end -1 refers to the last item, -2 refers to the second last item etc. Example… Read More »

11. Lists

mylist = [“apple”, “banana”, “cherry”] List Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets: Example Create a List:thislist = [“apple”, “banana”, “cherry”]print(thislist) Output:[‘apple’,… Read More »

10. Operators

Operators are used to performing operations on variables and values. In the example below, we use the + operator to add together two values: Example print(10 + 5) Output:15 Python divides the operators into the following groups: Arithmetic operators Assignment operators Comparison operators Logical operators Identity operators Membership operators Bitwise operators Python Arithmetic Operators Arithmetic operators are used with… Read More »