Divide a List by a Number in Python

Divide a List by a Number in Python

  1. Use a for Loop to Divide a List by a Number in Python
  2. Use a while Loop to Divide a List by a Number in Python
  3. Use List Comprehension to Divide a List by a Number in Python

Data is the most important part of any application. We store data in different forms such as arrays, lists, and objects and use them in different functions to store the required data in our database.

This article will explore the different ways to divide a list by a number. We will be using loops to iterate through lists and divide each element by a specific number and save the results into another list.

Use a for Loop to Divide a List by a Number in Python

First, we will be using a for loop to perform this task. Let’s go through an example in which we will create a list of numbers that we will divide by 5.

Example:

# python
listToDivide = [5,10,15,20,25,30,35,40,45,50]

print("List before dividing by 5: ",listToDivide)

newList = []

for items in listToDivide:
    new = items/5
    newList.append(int(new))

print("List after dividing by 5: ",newList)

Output:

Divide List Using for Loop

As you can see from the example above, we can easily divide a list by a specific number using the a for loop. But what if we want to save the data with no remainder left after being divided by that number?

Let’s use this concept in the following example. We will make 2 different lists to save numbers with and without remainders.

Example:

# python
listToDivide = [3,5,7,10,13,15,17,20,23,25,29,30,33,35,37,40,41,45,47,50]

print("List before dividing by 5: ",listToDivide)

newIntList = []
newFloatList = []

for items in listToDivide:
    if items % 5 == 0:
        newIntList.append(int(items))
    else:
        newFloatList.append(items)

print("List of numbers divisible by 5: ",newIntList)
print("List of numbers not divisible by 5: ",newFloatList)

Output:

Divide List Using for Loop and Save Into 2 Lists

As you can see in the example, we can even use this technique to separate data based on which numbers are divisible and not divisible by a specific number.

Use a while Loop to Divide a List by a Number in Python

Now, let’s discuss another method that can be used to divide a list by a number. In this method, we will use a while loop. So let’s use this loop with the same scenario as we discussed in our first example.

Example:

# python
listToDivide = [5,10,15,20,25,30,35,40,45,50]

print("List before dividing by 5: ",listToDivide)
newList = []
a = 0
while a < len(listToDivide):
    new = listToDivide[a]/5
    newList.append(int(new))
    a = a + 1
print("List after dividing by 5: ",newList)

Output:

Divide List Using while Loop

As you can see, we can easily divide a list by a specific number using the while loop. The results are the same as in the for loop.

Now, let’s use the concept in our second example and implement it by using the while loop and saving the results in 2 different lists based on whether the element is divisible by a number.

Now, let’s look at how we can use the while loop for the same purpose.

Example:

# python
listToDivide = [3,5,7,10,13,15,17,20,23,25,29,30,33,35,37,40,41,45,47,50]

print("List before dividing by 5: ",listToDivide)

newIntList = []
newFloatList = []
a = 0
while a < len(listToDivide):
    if listToDivide[a] % 5 == 0:
        newIntList.append(int(listToDivide[a]))
        a = a + 1
    else:
        newFloatList.append(listToDivide[a])
        a = a + 1

print("List of numbers divisible by 5: ",newIntList)
print("List of numbers not divisible by 5: ",newFloatList)

Output:

Divide List Using while Loop and Save Into 2 Lists

As you can see, we can implement the same logic with both for and while loops and get the same results. Loops make it easier for us to iterate through each list element and perform any task with it as we want.

Use List Comprehension to Divide a List by a Number in Python

Another way to divide a list by a number is using list comprehension. This method is a one-line method; we write the code in one line so that it is executed.

It is a very complex method to use in our second example, in which we will be separating the elements based on whether they are divisible by a specific number or not.

So, let’s use list comprehension to divide a list by a number, as shown below.

Example:

# python
listToDivide = [5,10,15,20,25,30,35,40,45,50]

print("List before dividing by 5: ",listToDivide)

newList = []


[newList.append(int(i/5)) for i in listToDivide]

print("List after dividing by 5: ",newList)

Output:

Divide List Using List Comprehension

As you can see from the example above, we can also use list comprehension to divide a list by a number.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python