I am trying to iterate over a list of floats in python which look like this

```
[3.09312635991831, 2.685707263863166, 1.7249614343269735, 2.551923173039131, 2.3768648392514904]
```

and plug them into a function handle but I cant seem to iterate over the list of floats. I get the error ‘float’ object is not iterable.

Here is what I am currently working with. Where `xpoint`

and `ypoint`

are the lists of floats and `func`

is the expression `func = lambda x,y: x**2 + y**2`

.

```
def monte_carlo_integral(a,b,c,n,func):
T = triangle_area(a, b, c)
left = T*(1/n)
points = random_numbers(a, b, c, n, 1)
xpoint = [item[0] for item in points]
ypoint = [item[1] for item in points]
for i in range(n):
right = sum(func(xpoint[i],ypoint[i]))
print(right)
```

I reproduced the problem here.

```
func = lambda x,y: x**2 + y**2
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291, 1.2534822780679544, 2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161, 1.8353208404114492, 3.868876505529935]
for i in range(5):
summation = sum(func(x[i],y[i]))
print(summation)
```

Stack Overflow Asked by JoeyHoward988 on January 27, 2021

4 Answers```
func = lambda x,y: x**2 + y**2
for i in range(5):
summation = sum(func(x[i],y[i]))
```

Your problem is that `func(x[i],y[i])`

returns a single float. You then try to call `sum`

on this singular float.

What you probably want instead is this:

```
func = lambda x,y: x**2 + y**2
summation = 0
for i in range(5):
summation += func(x[i],y[i])
```

Or even better, this:

```
func = lambda x,y: x**2 + y**2
summation = sum(func(x[i],y[i]) for i in range(5))
```

Or even better, this:

```
summation = sum(xi**2 + yi**2 for xi, yi in zip(x, y))
```

Also note the `math.hypot`

function.

Correct answer by orlp on January 27, 2021

You need to return

x**2

and

y**2

which you can them sum as follows:

```
func = lambda x,y: ((x**2),(y**2))
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291, 1.2534822780679544, 2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161, 1.8353208404114492, 3.868876505529935]
for a,b in zip(x,y):
summation += sum(func(a,b))
print(summation) # 92.91993379273195
```

Answered by Aaj Kaal on January 27, 2021

You probably want one of the two solutions Here

Setup:

```
func = lambda x,y: x**2 + y**2
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291, 1.2534822780679544,
2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161, 1.8353208404114492,
3.868876505529935]
```

Solution One:

```
summation = 0
for i in range(5):
summation += func(x[i],y[i])
```

Solution Two :

```
summation = sum((func(x[i],y[i]) for i in range(5)))
```

Print result:

```
print(summation)
```

Answered by RufusVS on January 27, 2021

The problem I think is in this line

```
right = sum(func(xpoint[i],ypoint[i]))
```

the argument of the function `sum`

must be an iterable object, but you're
passing a `float`

. Remember that `ight = sum(func(xpoint[i],ypoint[i]))`

returns
the sum of the squares the two coordinates.

Answered by Gealber on January 27, 2021

1 Asked on January 12, 2021 by karthik

2 Asked on January 12, 2021

3 Asked on January 12, 2021 by redouane-nouh

0 Asked on January 12, 2021 by mario-tunon

1 Asked on January 12, 2021 by nachengue

1 Asked on January 12, 2021 by vivz

1 Asked on January 12, 2021 by user13380366

1 Asked on January 11, 2021 by mr-spock

2 Asked on January 11, 2021 by edi_hadzic

2 Asked on January 11, 2021 by steven85791

1 Asked on January 11, 2021 by eka-buyung-lienadi

1 Asked on January 11, 2021 by york910

3 Asked on January 11, 2021 by alejandro-navas

1 Asked on January 11, 2021 by brian-yeh

Get help from others!

Recent Questions

- MouseLook Script “Pops” back to the last value when the script is enabled after being disabled or destroyed
- Unity app crashes when using unmodified custom Android manifest (didn’t find class “UnityPlayerActivity”)
- How do i draw a ray in unity
- How to test consistency of responses?
- How can I understand these variograms?

Recent Answers

- kjetil b halvorsen on How to test consistency of responses?
- eric_kernfeld on How to test consistency of responses?
- DMGregory on MouseLook Script “Pops” back to the last value when the script is enabled after being disabled or destroyed
- Philipp on How do i draw a ray in unity
- Justin Markwell on Unity app crashes when using unmodified custom Android manifest (didn’t find class “UnityPlayerActivity”)

© 2022 AnswerBun.com. All rights reserved.