How do you divide each element in a list by an int?

Python

Python Problem Overview


I just want to divide each element in a list by an int.

myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = myList/myInt

This is the error:

TypeError: unsupported operand type(s) for /: 'list' and 'int'

I understand why I am receiving this error. But I am frustrated that I can't find a solution.

Also tried:

newList = [ a/b for a, b in (myList,myInt)]

Error:

ValueError: too many values to unpack

Expected Result:

newList = [1,2,3,4,5,6,7,8,9]


EDIT:

The following code gives me my expected result:

newList = []
for x in myList:
    newList.append(x/myInt)

But is there an easier/faster way to do this?

Python Solutions


Solution 1 - Python

The idiomatic way would be to use list comprehension:

myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = [x / myInt for x in myList]

or, if you need to maintain the reference to the original list:

myList[:] = [x / myInt for x in myList]

Solution 2 - Python

The way you tried first is actually directly possible with numpy:

import numpy
myArray = numpy.array([10,20,30,40,50,60,70,80,90])
myInt = 10
newArray = myArray/myInt

If you do such operations with long lists and especially in any sort of scientific computing project, I would really advise using numpy.

Solution 3 - Python

>>> myList = [10,20,30,40,50,60,70,80,90]
>>> myInt = 10
>>> newList = map(lambda x: x/myInt, myList)
>>> newList
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Solution 4 - Python

The abstract version can be:

import numpy as np
myList = [10, 20, 30, 40, 50, 60, 70, 80, 90]
myInt = 10
newList  = np.divide(myList, myInt)

Solution 5 - Python

myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = [i/myInt for i in myList]

Solution 6 - Python

myInt=10
myList=[tmpList/myInt for tmpList in range(10,100,10)]

Solution 7 - Python

I was running some of the answers to see what is the fastest way for a large number. So, I found that we can convert the int to an array and it can give the correct results and it is faster.

  arrayint=np.array(myInt)
  newList = myList / arrayint

This a comparison of all answers above

import numpy as np
import time
import random
myList = random.sample(range(1, 100000), 10000)
myInt = 10
start_time = time.time()
arrayint=np.array(myInt)
newList = myList / arrayint
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
newList = np.array(myList) / myInt
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
newList = [x / myInt for x in myList]
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
myList[:] = [x / myInt for x in myList]
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
newList = map(lambda x: x/myInt, myList)
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
newList = [i/myInt for i in myList]
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
newList  = np.divide(myList, myInt)
end_time = time.time()
print(newList,end_time-start_time)
start_time = time.time()
newList  = np.divide(myList, myInt)
end_time = time.time()
print(newList,end_time-start_time)

Solution 8 - Python

list = [2,4,8]  
new_list = [x//2 for x in list]  
print(new_list)  

Output:

[1, 2, 4]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCasaView Question on Stackoverflow
Solution 1 - PythonsoulcheckView Answer on Stackoverflow
Solution 2 - PythonsilvadoView Answer on Stackoverflow
Solution 3 - PythonDogbertView Answer on Stackoverflow
Solution 4 - PythonArminView Answer on Stackoverflow
Solution 5 - PythonNotCamelCaseView Answer on Stackoverflow
Solution 6 - PythonRichardView Answer on Stackoverflow
Solution 7 - PythonI_Al-thamaryView Answer on Stackoverflow
Solution 8 - PythonPavan Kalyan GundaView Answer on Stackoverflow