Divide List Elements by Integer in Python (Example)
In this tutorial, you’ll learn how to divide each element of a list by an integer in the Python programming language.
The table of contents is structured as follows:
Let’s dive into it!
Example
At times, you may need to divide each element of a list by an integer in Python. This operation is useful when you want to distribute or scale the values in the list based on a specific factor.
To divide each element of a list by an integer in Python, you can follow these steps:
Step 1: Declare the list.
Step 2: Define the integer value.
Step 3: Divide each element by the integer value.
Here’s an example of dividing list elements by an integer:
# Step 1: Declare the list my_list = [10, 20, 30, 40, 50] # Step 2: Define the integer value divisor = 5 # Step 3: Divide each element by the integer value for i in range(len(my_list)): my_list[i] //= divisor # Print the updated list print(my_list) # Output: [2, 4, 6, 8, 10]
In the above example, we have a list named my_list
containing integer values. We defined an integer value divisor
as 5. Then, using a for loop, we divided each element of the list by the integer value using the floor division operator //=
. Finally, we printed the updated list.
Video, Further Resources & Summary
Do you need more explanations on dividing list elements by an integer in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Check if List of Lists is Empty in Python
- Convert List from Character String to Integer in Python
- Check If Key Exists in List of Dictionaries in Python
- Access Dictionary within List in Python
- Python Programming Tutorials
This post has shown how to divide each element of a list by an integer in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.
Statistics Globe Newsletter