In the last article, you learned about methods used for replacing, adding and removing list items.
Read: How to Modify List Elements
In this article you’ll learn about organizing and copying a list. You'll also create numerical lists.
Organizing a List
Sort Method
This method sorts a list permanently. The changes made to a list cannot be reverted after using the sort method.
This method sorts the list in ascending order by default. To keep things simple, let’s create a separate word and number list.
To sort the list in descending order, pass in the argument reverse=True to the sort method.
Please, ensure to keep your words in the same format to avoid unexpected behavior. The sort method prioritizes uppercase words over lowercase.
In the first example, if we had put ‘riri’ in title case, the word would have come first in the list.
Sorted Function
This Function sorts a list temporarily. Changes are not made to the original list. It only creates a view of the sorted list.
We’ll use this function on our previous examples then print the sorted and original list.
As it can be seen, the original order of the list has not been changed.
The reverse argument can also be passed to the sorted function.
Reverse Method
This reverses the order of the original list. It changes the order of the list permanently.
As it can be seen, the list elements have been rearranged from the last to the first element
Copying a List
You might want to make a new list from an existing list. To make a copy of a list, the name of the existing list should be followed by an empty slice ([:]).
Remember that an omitted first and last index returns the whole list.
Let’ say you want to make a list of your favorite drinks from an existing list of drinks
As you can see, the list has been copied successfully. You can now proceed to modify the list to include just your favorite drinks.
As shown by the output, the original list has not been changed.
Simply referencing the list without slice would always affect the original list.
To copy a list, make sure you include the slice.
Range Function
Numerical lists can be created with the range function. It takes two arguments, the start index and end index, and an optional third argument, the stride.
These terms were explained when we worked with slices.
Read: How to Access List Elements
The arguments in the range function are separated with comma. To create a list of 1 to 5 inclusive, we’ll choose a start index of 1 and an end index of 6. Remember that the end index position is not included.
The list function converts the range object to a list. You’ll learn more about type conversion in future sessions.
Let’s create a list of all odd numbers between 1 and 10 inclusive.
We’ll choose a start index of 1, an end index of 11 and a stride of 2
Try creating a list of even numbers between 1 and 50 using the range function.
In the next article, you’ll learn about other python data structures – Tuples, Sets and Dictionaries.
Summary
- The sort method sorts a list permanently
- The sorted method sorts a list temporarily – without changing the order of the original list
- The reverse method reverses the order of a list permanently
- A list can be copied using an empty slice
- Numerical lists can be created using the range function
No comments: