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

Advertisement

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

### Question

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?

2015/10/22

### Accepted Answer

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]
```

2014/10/09

Read more... Read less...

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.

2014/10/30

```
>>> 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]
```

2011/11/23

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

2011/11/23

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)
```

2018/10/03

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow

Email: [email protected]