How do you divide each element in a list by an int?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands
--
Chapters
00:00 Question
01:02 Accepted answer (Score 297)
01:24 Answer 2 (Score 98)
01:49 Answer 3 (Score 28)
02:01 Answer 4 (Score 13)
02:16 Thank you
--
Full question
https://stackoverflow.com/questions/8244...
Answer 1 links:
[numpy]: http://numpy.org
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands
--
Chapters
00:00 Question
01:02 Accepted answer (Score 297)
01:24 Answer 2 (Score 98)
01:49 Answer 3 (Score 28)
02:01 Answer 4 (Score 13)
02:16 Thank you
--
Full question
https://stackoverflow.com/questions/8244...
Answer 1 links:
[numpy]: http://numpy.org
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ACCEPTED ANSWER
Score 304
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]
ANSWER 2
Score 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.
ANSWER 3
Score 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]
ANSWER 4
Score 11
myList = [10,20,30,40,50,60,70,80,90]
myInt = 10
newList = [i/myInt for i in myList]