0

I am trying to find the mean of a list that contains the types int, float, and str. When I iterate through and print each item, the code works.

mylist = ["hello", 89.21, -3, "goodbye", 21, 0.0056, -12.34, "thank you", "please", 999.44409]
newlist = []
for i in mylist:
    print(i)

and the result is

hello
89.21
-3
goodbye
21
0.0056
-12.34
thank you
please
999.44409

But then when I try to operate on each "i" it gives me the error "TypeError: 'float' object is not iterable". I am trying to iterate over each item, not each character.

    for i in mylist:
    if type(i)== int or type(i)== float:
        newlist += i

Can anyone steer me in the right direction?

7 Answers 7

2

Try this:

for i in mylist: 
  if type(i)== int or type(i)== float:
    newlist.append(i) 
Sign up to request clarification or add additional context in comments.

Comments

2

the line

for i in myList

Means it goes through myList, or iterates through it.

example. if your string was "hello",

and you did the += thing in the last line of the last snippet,

The newList would become ['h','e','l','l','o'], which is not desired.

Floats aren't iterable, as stated in the Traceback (aka error message)

Therefore, you have to append the i into the new list that you want. That'll add it to the list.

So for that little snippet, you can do:

for i in mylist: 
    if type(i)== int or type(i)== float:
        newlist.append(i) 

Comments

1

Your problem results from what happens when you try to add something to a list. Here is an example:

>>> i = []
>>> i += 'hello'
>>> i
['h', 'e', 'l', 'l', 'o']

You can see that when you try to add something to a list, each individual "item" of whatever is on the right of the += operand is added separately to the list. This ability to separate an item into its components is called iteration, and when an object supports this we say the object is iterable.

The list object relies on this ability to iterate an object when trying to add things to a list.

Floats (and ints) are not iterable which is why you get the error. To fix the problem (as others have suggested), use the append() method, which just takes the item given in the argument and adds it as is to the list:

>>> i.append('hello')
>>> i
['h', 'e', 'l', 'l', 'o', 'hello']
>>> i.append(10)
>>> i
['h', 'e', 'l', 'l', 'o', 'hello', 10]

Comments

0

Replace this:

newlist += i

with:

newlist += [i]     

demo:

>>> [3.0] +4.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "float") to list

>>> [3.0] + [4.0]
[3.0, 4.0]

you can also you list.append to append elements at the end of list

newlist.append(i)

Comments

0

newlist is a list here, Actually you are adding list reference with the value.

Instead of that, try this

newlist.append(i) 

Comments

0

Ideally if you are looking to check if the type is a number such as int or float while iterating then you'll want to use isinstance, doing something such as:

import numbers

mylist = ["hello", 89.21, -3, "goodbye", 21, 0.0056, -12.34, "thank you", "please", 999.44409]
newlist = []
for i in mylist:
    if isinstance(i, (int, float)):
        newlist += [i]
print(newlist)

Output:

[89.21, -3, 21, 0.0056, -12.34, 999.44409]

Comments

0

When you try to do a newlist += i, python complains because this operation is valid only if i itself was a list like newlist or any other iterable like a tuple. Then this operation would have combined the results and stored the result in newlist.

To add i to the newlist, you should use newlist.append(i) instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.