Divide list by number python
I just want to divide each element in a list by an int. Show
This is the error:
I understand why I am receiving this error. But I am frustrated that I can't find a solution. Also tried:
Error:
Expected Result:
The following code gives me my expected result:
But is there an easier/faster way to do this? Summary: The most Pythonic approach to divide each element in a list is to use the following list comprehension: Read ahead to discover numerous other solutions. Problem: How to divide each element in a list and return a resultant list containing the quotients? Example: li = [38, 57, 76, 95, 114, 161.5] num = 19 # Some way to divide each element of li with 19 Expected Output:
So, without further delay, let us dive into the mission-critical question and find out the different ways of solving it.
Video WalkthroughHow to Divide Each Element in a List in Python Method 1: Using a For LoopApproach:
Code: li = [38, 57, 76, 95, 114, 161.5] num = 19 res = [] for val in li: res.append(val/num) print(res) Output:
📌Read Here: Python Loops Method 2: Using a List ComprehensionLet’s dive into the most Pythonic solution to the given problem. Approach: Create a list comprehension such that:
Code: li = [38, 57, 76, 95, 114, 161.5] num = 19 res = [a/num for a in li] print(res) Output:
💎A quick recap to List Comprehensions in Python: List comprehension is a compact way of creating lists. The simple formula is 📌Recommended Read: List Comprehension in Python — A Helpful Illustrated Guide Method 3: Using map and lambdaApproach: The idea here is to use an anonymous Code: li = [38, 57, 76, 95, 114, 161.5] num = 19 res = list(map(lambda x: x/num, li)) print(res) Output:
💎Readers Digest:
Mastering the Python Map Function [+Video] 📌Read more about
Let's Play Finxter - The Lambda Function in Python 📌Read more about Method 4: Using NumpyAnother simple workaround for the given problem is to use the 4.1 Using division / operator
Code: import numpy as np li = [38, 57, 76, 95, 114, 161.5] arr = np.array(li) num = 19 res = arr/num print(res.tolist()) Output:
4.2 Using numpy.divide()
Code: import numpy as np li = [38, 57, 76, 95, 114, 161.5] arr = np.array(li) num = 19 res = np.divide(arr, num) print(res.tolist()) Output:
💎A Quick Recap to numpy.divide() The numpy.divide() method returns an element-wise true division of the inputs in the given array. Syntax:
Here:
✨When you have multiple division processes going on, you can accelerate it significantly by using NumPy division. Not only does it allow you to perform element-wise division but this also works on multi-dimensional NumPy arrays. For example: import numpy as np # Create 2D lists a = [[1, 2, 3], [4, 5, 6]] b = [[2, 4, 6], [8, 10, 12]] # Convert lists to 2D NumPy arrays a = np.array(a) b = np.array(b) # Divide the 2D arrays print(a / b) Output:
📌Related Article: The Ultimate Guide to NumPy Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.) ConclusionWe have successfully learned four different ways of dividing elements in a given list with a given number. I hope this tutorial helped to answer all your queries. Please subscribe and stay tuned for more interesting tutorials. Happy learning! 🙂 Web Scraping with BeautifulSoup One of the most sought-after skills on Fiverr and Upwork is web scraping . Make no mistake: extracting data programmatically from web sites is a critical life-skill in today’s world that’s shaped by the web and remote work. This course teaches you the ins and outs of Python’s BeautifulSoup library for web scraping. I am a professional Python Blogger and Content creator. I have published numerous articles and created courses over a period of time. Presently I am working as a full-time freelancer and I have experience in domains like Python, AWS, DevOps, and Networking. You can contact me @: UpWork How do you divide a list by a number in Python?Create an empty list that will store the quotients. Iterate across all the elements in the given list using a for loop. Divide each element with the given number/divisor and append the result in the resultant list. Finally, display the resultant list after all the quotients have been calculated and appended to it.
How do you split a list into N parts in Python?array_split() to split a list into n parts. Call numpy. array_split(list, n) to return a list of n NumPy arrays each containing approximately the same number of elements from list . Use the for-loop syntax for array in list to iterate over each array in list .
|