• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Divide Each Element in List by Scalar Value with Python

Divide Each Element in List by Scalar Value with Python

May 5, 2022 Leave a Comment

To divide a list by a scalar in Python, the easiest way is with list comprehension.

list_of_numbers = [1, 5, 2, 4]

print([num / 3 for num in list_of_numbers])

#Output:
[0.3333333333333333, 1.6666666666666667, 0.6666666666666666, 1.3333333333333333]

You can also use the Python map() function to apply a function and divide a list by a scalar.

list_of_numbers = [1, 5, 2, 4]

def divide_by_3(x):
    return lst / 3

print(list(map(divide_by_3,list_of_numbers)))

#Output:
[0.3333333333333333, 1.6666666666666667, 0.6666666666666666, 1.3333333333333333]

When working with collections of data, the ability to easily manipulate and change the values in those collections is valuable.

One example of this is if you have a list of numbers and want to divide each element in the list by a scalar value.

The easiest way to divide a list by a scalar in Python is with list comprehension.

List comprehension allows us to loop over a list, operate on each element, and create a new list.

Below is a simple example of how you can divide a list by 3 in Python with list comprehension.

list_of_numbers = [1, 5, 2, 4]

print([num / 3 for num in list_of_numbers])

#Output:
[0.3333333333333333, 1.6666666666666667, 0.6666666666666666, 1.3333333333333333]

Using map() to Divide Elements in a List by a Scalar Number in Python

The Python map() function allows you to apply a function to each element of a list.

We can use map() to divide all elements in a list by a number.

map() takes the name of a function, or a lambda function, and a list, and returns a map object which can be converted to a list.

Below is an example of how you can use map() to divide the elements of a list by a number in Python.

list_of_numbers = [1, 5, 2, 4]

def divide_by_3(x):
    return lst / 3

print(list(map(divide_by_3,list_of_numbers)))

#Output:
[0.3333333333333333, 1.6666666666666667, 0.6666666666666666, 1.3333333333333333]

How to Multiply List by Scalar with Python

If you want to go the other way, and multiply each number in a list by another number, then you can take the code from above and easily perform this operation with list comprehension.

Below is a simple example of how you can multiply a list by 3 in Python with list comprehension.

list_of_numbers = [1, 5, 2, 4]

print([num * 3 for num in list_of_numbers])

#Output:
[3, 15, 6, 12]

Hopefully this article has been useful for you to learn how to divide a list by a scalar with Python.

Other Articles You'll Also Like:

  • 1.  pandas min – Find Minimum Value of Series or DataFrame
  • 2.  How to Check if String Contains Uppercase Letters in Python
  • 3.  How to Return Nothing in Python from Function
  • 4.  Using Python to Read File Character by Character
  • 5.  Creating a Random Color Turtle in Python
  • 6.  Remove Leading Zeros from String with lstrip() in Python
  • 7.  PROC MEANS Equivalent in Python
  • 8.  Reverse a List in Python Without Reverse Function
  • 9.  Sorting with Lambda Functions in Python
  • 10.  Print Time Elapsed in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy