• author: Python and Pandas with Reuven Lerner

Exploring the Python Standard Library Series: Methods to Change Lists

In this video explainer, Reuben Lerner takes viewers through two essential methods to change lists in Python. These methods can be used to reverse or sort a list based on specific needs, and can be applied in a variety of scenarios.

1. The Reverse Method

Reversing a list is a simple and common operation in Python. The reverse method offers a built-in way to change the order of items in a list. When my_list.reverse() is called, it reverses the list in place, meaning the original items are modified rather than returning a new list. It's important to note that reverse does not return anything, only modifies the list.

For instance, let's say we have a list of numbers as follows: my_list = [10, 20, 30, 40, 50]. To reverse this list, we can use the reverse method by calling my_list.reverse(). The items in the list will be modified in reverse order from 50, 40, 30, 20, 10.

Two crucial things to consider while working with the reverse method are:

  • The method mutates the list by changing the original order of items.
  • The method returns None and shouldn't be assigned back to the original list; doing so will result in an empty list, i.e., my_list = my_list.reverse() is incorrect, and it should be my_list.reverse().

2. The Sort Method

Sorting is another common operation used on lists to sort the items in ascending or descending order. Similar to the reverse method, the sort method changes the order of items in a list and does not return anything.

Taking the same my_list as an example, we can sort them in a specific order using the sort method. To sort list items from smallest to largest, the my_list.sort() is used. Similarly, if we need to sort from largest to smallest, we define the reverse parameter by its boolean value as my_list.sort(reverse=True).

It's notable that the sort method does not work with mixed type lists; it only works with lists of the same type. Hence my_list = ['5', 'a', '6', 3] can't be sorted by the conventional sort method.

However, the sort method accepts any function that can generate a comparable value for each element in the list through the key parameter. Outside of this, the sort method can sort based on attribute of objects such as sort(key=lambda x: x.attr).

3. Functional Versions

In addition to the sort and reverse methods, Python contains functional versions of these methods used to apply sorting and reversing on any iterable object, i.e., tuples, or a string, etc.

The reversed function takes an iterable and returns an iterator of the reversed items of the original iterable. Similarly, the sorted function returns a new sorted list based on the iterable passed as an argument.

Keep in mind that the reversed function returns an iterator that has to be converted to a list by using list(reversed(my_list)), whereas the sorted function returns a new list on its own.

4. Tips

Lastly, it's essential to differentiate between the reversed function and the reverse method as they sound similar. reverse is a method used to modify the original list of items, whereas reversed returns an iterator of the iterable reverse.

Conclusion

Python's standard library is an abundant repository of reusable and highly efficient codes. In conclusion, learning these essential methods will help redefine how we utilize lists in Python. Reuben Lerner's video explainer offers easy-to-follow instructions on these methods, making Python development even more exciting for programmers of all levels.

Previous Post

How to Use NetLogo Modeling Commons Groups and Permissions

Next Post

Understanding Three Different Types of Errors When Running a Python Function

About The auther

New Posts

Popular Post