By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,603 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,603 IT Pros & Developers. It's quick & easy.

TypeError: 'int' object is not iterable

P: 1
Expand|Select|Wrap|Line Numbers
  1. def remove_duplicates(xlist):
  2.     new_list = []
  3.     return [x*y for x,y in xlist if y not in new_list.iteritems()]
  4.  
  5. print remove_duplicates([4,5,5,6])
i am trying to remove repeating numbers from a list that can be any size. but when I test out this idea i hit a brick wall. I get TypeError: 'int' object is not iterable. I have tried several alternatives as a single liner but was unsuccessful. Please can somone help me.
Jan 8 '14 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
What end result are you looking for? The code segment for x,y in [1,2,3,4] will always fail.
Expand|Select|Wrap|Line Numbers
  1. >>> for x,y in [1,2]:
  2. ...     print x,y
  3. ...     
  4. Traceback (most recent call last):
  5.   File "<interactive input>", line 1, in <module>
  6. TypeError: 'int' object is not iterable
  7. >>> 
If you want to remove duplicates from xlist and keep the order of the sequence:
Expand|Select|Wrap|Line Numbers
  1. >>> output = []
  2. >>> xlist = [1,2,3,5,5,5,6,6,7,8,8]
  3. >>> for item in xlist:
  4. ...     if item not in output:
  5. ...         output.append(item)
  6. ...         
  7. >>> output
  8. [1, 2, 3, 5, 6, 7, 8]
  9. >>> 
Are you trying to do this?
Expand|Select|Wrap|Line Numbers
  1. >>> output
  2. [1, 2, 3, 5, 6, 7, 8]
  3. >>> reduce(lambda x,y: x*y, output)
  4. 10080
  5. >>> 
Jan 8 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.