Sorting Lists in Python (4 Examples)
Updated: Jun 12, 2023
The Fundamentals The Pythonic way to sort elements in a Python list is to use the sort() method or the sorted() function. These two are almost the same, except that the sort() method modifies the original list in place while the......
Using the list.insert() method in Python
Updated: Jun 12, 2023
Overview In Python, the list.insert() method inserts an element at a given position in a list. Below is its syntax: list.insert(i, x) Parameters: i: The index of the element before which to insert, and can be any integer.......
Python: How to Calculate the Average of a Numeric List
Updated: Jun 11, 2023
The main idea of calculating the average of a numeric list (a list that contains only numbers) can be described in two steps: Find the total sum of all elements of the list. Divide the total sum by the number of elements. There......
Python: 4 Ways to Find the Sum of a Numeric List
Updated: Jun 11, 2023
This pithy, example-based article will walk you through a couple of different ways to calculate the sum of a numeric list (a list that contains only numbers) in Python. We’ll also discuss the performance of each approach so you can......
Python: Find the Min and Max in a Numeric List (4 Ways)
Updated: Jun 10, 2023
In this concise example-based article, we will go through some techniques to find the maximum and minimum elements in a numeric list (a list that contains only numbers) in Python. Besides that, we will also discuss the performance of......
How to Reverse a List in Python (with Examples)
Updated: Jun 10, 2023
This succinct, example-based article shows you the best and most Pythonic approaches to reversing a given list in Python (in terms of performance and code readability). Using the reverse() method The reverse() method reverses the......
Python: How to Find All Occurrences of a Value in a List
Updated: Jun 10, 2023
This concise, straightforward article will walk you through a few different ways to find all occurrences of a certain value in a list in Python. The desired result will be a list of indices of found elements. Let’s get......
Python: How to Remove Elements from a List (4 Approaches)
Updated: Jun 10, 2023
This concise, straightforward article will walk you through a couple of different ways to remove one or many elements from a list in Python 3. Using the remove() method The remove() method helps you remove a specific element from a......
Python: Replacing/Updating Elements in a List (with Examples)
Updated: Jun 06, 2023
This concise, example-based article gives you some solutions to replace or update elements in a list in Python. Using indexing or slicing If you want to update or replace a single element or a slice of a list by its index, you can......
Python: 3 Ways to Select Random Elements from a List
Updated: Jun 06, 2023
This succinct and practical article shows you a couple of different ways to randomly select one or multiple elements from a given list in Python. Without any further ado, let’s get started. Using random.choices() and......
Ways to Combine Lists in Python
Updated: Jun 06, 2023
This succinct and practical article walks you through a couple of different ways to combine multiple lists in Python. Each approach will be accompanied by a clear illustrative example. Without any further ado, let’s get......
Python: How to Check If a List Is Empty (3 Approaches)
Updated: Jun 06, 2023
In Python, a list is a data structure that can store multiple values of different types in a sequential order. An empty list is a list that contains no elements or items. It is a falsy value. This short article will show you the most......