204

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?

jesterjunk
  • 2,342
  • 22
  • 18
Casa
  • 2,247
  • 2
  • 15
  • 11

8 Answers8

300

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]
soulcheck
  • 36,297
  • 6
  • 91
  • 90
98

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.

silvado
  • 17,202
  • 2
  • 30
  • 46
  • 8
    I know this is an old reply but for anyone still reading it: keep in mind that when using numpy.array you should specify the type for example `numpy.array([10,20,30,40,50,60,70,80,90], dtype='f')`for loat. Otherwise dividing by 3 would give you just 3 as the result instead of 3.333.. – Richard Boonen Jul 31 '17 at 11:52
  • 3
    @RichardBoonen in this case the OP wanted to do int division, but if you want to do float division, you're right, you have to specify the type to numpy. Or put a single float in the list: ``numpy.array([10.,20,30,40,50,60,70,80,90])`` – silvado Aug 02 '17 at 18:53
28
>>> 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]
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • 1
    Do you think map is better than list comprehension in this case? I am just wondering as I would of gone for list comprehension, as it is easier to read. – Andrew Cox Nov 23 '11 at 15:44
  • @AndrewCox I prefer map (coming from a non python background). List comprehension seems to be cleaner to me too, so you should probably go with that. – Dogbert Nov 23 '11 at 15:46
  • Do you know if this is faster than the solution soulcheck and berkantk posted? – Casa Nov 23 '11 at 15:55
  • @Casa: Someone tested this at http://stackoverflow.com/q/1247490 . The conclusion seems to be that list comprehensions win, in this particular case. – Brian Nov 23 '11 at 16:15
  • 5
    Nowadays `map()` returns a map object, so if you want a list you have to explicitly say `list()`. So in this case: `newList = list(map(lambda x: x/myInt, myList))` – lagrange103 May 13 '17 at 01:10
14

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)
Joel
  • 1,564
  • 7
  • 12
  • 20
Armin
  • 331
  • 5
  • 11
11
myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = [i/myInt for i in myList]
NotCamelCase
  • 1,073
  • 1
  • 10
  • 18
0
myInt=10
myList=[tmpList/myInt for tmpList in range(10,100,10)]
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Richard
  • 21
  • 1
0

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(end_time-start_time)
start_time = time.time()
newList = np.array(myList) / myInt
end_time = time.time()
print(end_time-start_time)
start_time = time.time()
newList = [x / myInt for x in myList]
end_time = time.time()
print(end_time-start_time)
start_time = time.time()
myList[:] = [x / myInt for x in myList]
end_time = time.time()
print(end_time-start_time)
start_time = time.time()
newList = map(lambda x: x/myInt, myList)
end_time = time.time()
print(end_time-start_time)
start_time = time.time()
newList = [i/myInt for i in myList]
end_time = time.time()
print(end_time-start_time)
start_time = time.time()
newList  = np.divide(myList, myInt)
end_time = time.time()
print(end_time-start_time)
start_time = time.time()
newList  = np.divide(myList, myInt)
end_time = time.time()
print(end_time-start_time)
Community
  • 1
  • 1
I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
0
list = [2,4,8]  
new_list = [x//2 for x in list]  
print(new_list)  

Output:

[1, 2, 4]
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '22 at 07:28
  • using `x//2` does an integer division – Freddy Mcloughlan May 14 '22 at 03:32