Waste for a long time, to tell you the truth, I'm really not good at writing, review the list today.
The list consists of a series of elements arranged in a specific order. Lists can contain almost all data types. Previous strings and numbers can appear in lists. In Python, the list is represented by square brackets ([]) and the elements are separated by commas.
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles)```
Python prints the internal representation of the list, including square brackets:
['trek', 'cannondale', 'redline', 'specialized']
Lists are ordered collections, so to access any element of the list, you just need to tell Python the location or index of that element. To access list elements, indicate the name of the list, then indicate the index of the element, and place it in square brackets.
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0])
Python returns only that element, not square brackets and quotation marks: (index starts at 0 instead of 1)
trek
Python provides a special syntax for accessing the last list element. By specifying the index as - 1, Python can return the last list element:
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[-1])
These codes return'special'
Modify, add, and delete elements
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles[0] = 'ducati' print(motorcycles)
Let's first define a list of motorcycles, the first element of which is'honda'. Next, we change the value of the first element to'ducati'. The output shows that the value of the first element does change, but that of the other list elements does not.
['honda', 'yamaha', 'suzuki'] ['ducati', 'yamaha', 'suzuki']
When adding new elements to a list, the easiest way is to append elements to the end of the list. Continue to use the list in the previous example and add a new element'ducati'at the end:
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles.append('ducati') print(motorcycles)
Method append() adds the element'ducati'to the end of the list
['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha', 'suzuki', 'ducati']
insert() can be used to add new elements anywhere in the list. To do this, you need to specify the index and value of the new element.
motorcycles = ['honda', 'yamaha', 'suzuki'] motorcycles.insert(0, 'ducati') print(motorcycles)
In this example, the value'ducati'is inserted at the beginning of the list; the method insert() adds space at index 0 and stores the value'ducati' there. This action moves each existing element in the list to the right:
['ducati', 'honda', 'yamaha', 'suzuki']
Delete elements from the list
If you know the location of the element to be deleted in the list, you can use the del statement.
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) del motorcycles[0] print(motorcycles)
del deletes the first element in the list motorcycles -'honda':
['honda', 'yamaha', 'suzuki'] ['yamaha', 'suzuki']
Method pop() deletes the elements at the end of the list and allows you to use it again. The term pop comes from the analogy that lists are like a stack, and deleting elements at the end of a list is equivalent to popping up elements at the top of a stack. Here's a motorcycle pop-up from the list of motorcycles:
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) popped_motorcycle = motorcycles.pop() print(motorcycles) print(popped_motorcycle)
The output shows that the value'suzuki'at the end of the list has been deleted and is now stored in the variable popped_motorcycle:
['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha'] suzuki
In fact, you can use pop() to delete elements anywhere in the list by specifying the index of the elements to be deleted in parentheses.
motorcycles = ['honda', 'yamaha', 'suzuki'] first_owned = motorcycles.pop(0) print('The first motorcycle I owned was a ' + first_owned.title() + '.')
Output is a simple sentence describing the first motorcycle I bought:
The first motorcycle I owned was a Honda.
Delete elements by value
Sometimes, you don't know where the values you want to delete from the list are. If you only know the value of the element to be deleted, you can use the method remove(). For example, suppose we want to delete the value'ducati'from the list motorcycles.
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) motorcycles.remove('ducati') print(motorcycles)
Python determines where'ducati'appears in the list and deletes the element:
['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki']
When you use remove() to delete an element from a list, you can then use its value. Next, delete the value'ducati'and print a message indicating the reason for deleting it from the list:
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) too_expensive = 'ducati' motorcycles.remove(too_expensive) print(motorcycles) print("\nA " + too_expensive.title() + " is too expensive for me.")
['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki'] A Ducati is too expensive for me.
Permanent sorting of lists using method sort()
The Python method sort() allows you to sort lists more easily. Suppose you have a list of cars and have them arranged alphabetically. To simplify this task, we assume that all values in the list are lowercase.
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars)
The method sort() permanently modifies the order of the list elements. Now cars are arranged in alphabetical order and can no longer be restored to their original order:
['audi', 'bmw', 'subaru', 'toyota']
You can also arrange the list elements in reverse alphabetical order, so you only need to pass the parameter reverse=True to the sort() method. The following example arranges the list of cars in reverse alphabetical order:
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort(reverse=True) print(cars)
Similarly, changes to the order of the list elements are permanent:
['toyota', 'subaru', 'bmw', 'audi']
Temporary sorting of lists using the sorted() function
The sorted() function allows you to display list elements in a specific order without affecting their original order in the list.
cars = ['bmw', 'audi', 'toyota', 'subaru'] print("Here is the original list:") print(cars) print("\nHere is the sorted list:") print(sorted(cars)) print("\nHere is the original list again:") print(cars)
First, we print the list in the original order (see and then display it alphabetically (see). After displaying the list in a specific order, we verify that the list elements are arranged in the same order as before.
Here is the original list: ['bmw', 'audi', 'toyota', 'subaru'] Here is the sorted list: ['audi', 'bmw', 'subaru', 'toyota'] Here is the original list again: ['bmw', 'audi', 'toyota', 'subaru']
Inverted Print List
To reverse the order of the list elements, use the method reverse(). Assuming that the list of cars is arranged according to the purchase time, it is easy to arrange the cars in the reverse order:
cars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) cars.reverse() print(cars)
reverse() does not refer to arranging list elements in reverse alphabetical order, but merely reverses the order of list elements:
['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']