• Register
1 vote
103 views

Problem:

Well, I am getting an error from my python program that says, argument of type 'int' is not iterable. 

Could anybody save my day by fixing the error? Find my sample code snippet below

def duplicates(n):
    my_list = n[0]
    for i in range(1,len(n)):
        if n[i] in my_list:
            print "duplicate " + str(n[i])
        else:
            out.append(n[i])    
    return my_list
duplicates([4,5,5,4])

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote
 
Best answer

Solution:

Once your compiler throws back an error, you should read the error message carefully. The error argument of type 'int' is not iterable means, you haven’t declared any list in your program but an integer. Whenever you write

my_list = n[0]

it becomes an integer. Hence my_list is an integer you can not write n[ i ] or my_list.append(n [ i ])

If you want to treat your my_list [ n ]  as a list you need to re-write the code like this:

my_list = [n[0]]

Good day!

13 9 6
94,240 points
0 votes

Solution:

Here c is the index not the list that you are looking. Because you cannot iterate through an integer, you are obtaining that error.

>>> myList = ['a','b','c','d']
>>> for c,element in enumerate(myList):
...     print c,element
... 
0 a
1 b
2 c
3 d

You are trying to check if 1 is in c, which does not make sense

Based on the OP's comment It must print "t" in case there is a 0 in a row and there is not a 1 in the row.

Alter if 1 not in c to if 1 not in row

for c, row in enumerate(matrix):
    if 0 in row:
        print("Found 0 on row,", c, "index", row.index(0))
        if 1 not in row: #change here
            print ("t")

More clarification: The row variable retain a single row itself, ie [0, 5, 0, 0, 0, 3, 0, 0, 0]. The c variable apprehend the index of which row it is. ie, in case row contains the 3rd row in the matrix, c = 2. Keep in mind that c is zero-based, for example the first row is at index 0, second row at index 1 etc.

Cast, Cast, Cast.

word = 'stuff'
blur = 12344566
word in str(blur)

This will output false successfully saying you whether stuff can be figured out inside the now string ‘12344566’. And currently for a more objective example.

array = ['Foo','Bar', 1]
for element in array:
    print(element)
    if 'sandwich' in element:
        print('Found the sandwich')

Here we can view all elements of the array are printed since the error happens on the third element. One will never trace a string in an int. You can eliminate checking types and error handling by wrapping element in str() to cast it as a string in case we come across any unruly data types.

array = ['Foo','Bar', 1]
for element in array:
    print(element)
    if 'sandwich' in str(element):
        print('Found the sandwich'

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 23 views
23 views
Problem: Hi there! I have been working in Python for a while. I am trying to run following program: value = 4.0 for val in value: number = float(input("Enter a number: ")) if number == 0: print("Zero") elif number > 0: print("Positive") elif ... understand what is causing this error. I want to take input from user unless val becomes 4. Could anyone from here please help me solve this error?
asked Jan 15 Code Learner 6.3k points
0 votes
1 answer 10 views
10 views
Problem: So I'm trying to get the words in a four line text document sorted alphabetically and remove any words already in it, by Here's my code: # call text file fname = raw_input("Enter file name: ") # open file fh = open(fname) # create list fh = list(fh) #create empty ... () # check words in list for word in line: if word in lst: continue else: lst = lst.append(word) lst = lst.sort() print lst
asked Jan 11 Mashhoodch 10.2k points
1 vote
1 answer 420 views
420 views
Problem: For the following code import re import string import pandas as pd import openpyxl import glob import csv import os import xlsxwriter import numpy as np keywords = {"oracles","mining","ico"} df = pd.read_csv(r"C:MYfile.csv", error_bad_lines=False) sep="," , ... j in df.columns] 21 TypeError: argument of type 'float' is not iterable Can anyone sortout the problem and help me to solve this?
asked Apr 26, 2020 samhaz 5k points
0 votes
1 answer 6 views
6 views
Problem: I am looking to make a loop so that for each element in the list, it will get broken down into each of it's characters. For example, say the number 137 was in the list then it would be turned into [1,3,7]. Then I want to add these numbers together (I ... it). However, I keep getting the error message TypeError: 'int' object is not iterable when I try and run this. What am I doing wrong?
asked Feb 23 Mashhoodch 10.2k points
0 votes
1 answer 17 views
17 views
Problem: I’m working on the count problem, and have come up with the following code: def count(sequence, item): found = 0 for i in len(sequence): if sequence[i] == item: found = found + 1 return found
asked Jan 22 Mashhoodch 10.2k points
1 vote
1 answer 128 views
128 views
Problem: I am doing a course on python programming language. As a begginer I have to do a lot of programming practice and some of them are very confusing. Today I wrote a code to do some counting problems in python. The code I wrote is: def count(serial, totals): ... is not iterable What is the problem in my programm and what should I do now. Can anyone explain me this error. Thanks in advance.
asked Apr 4, 2020 LizzyM 6.1k points
0 votes
1 answer 16 views
16 views
Program: I having trouble passing a function as a parameter to another function. This is my code: ga.py: def display_pageviews(hostname): pageviews_results = get_pageviews_query(service, hostname).execute() if pageviews_results.get('rows', []): pv = pageviews_results.get('rows') ... ='7daysAgo', end_date='today', metrics='ga:pageviews', sort='-ga:pageviews', filters='ga:hostname==%s' % hostname,)
asked Jan 11 Mashhoodch 10.2k points
0 votes
1 answer 1 view
1 view
Problem: How to I Solve this: python typeerror: 'nonetype' object is not iterable HELP! I need help understanding this: python typeerror: 'nonetype' object is not iterable Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked 1 hour ago Mashhoodch 10.2k points
0 votes
1 answer 18 views
18 views
Problem: What the traceback error is pointing out is the misuse of for statement: for i in Updt():. for in python 3 i "Python's for statement iterates? How function' object is not iterable. from getWords import getWords unique = [] def findUniqueWords(words): for word in words
asked Feb 15 Mashhoodch 10.2k points
1 vote
1 answer 429 views
429 views
Problem: When I run or compile this program import urllib2 import time import datetime HolaAndre = 'HUA','WALT','NOKA','CMG','EVAL','DARZ','SYM' def BackData(stock): try: print 'Still pulling',stock print str(datetime.datetime.fromtimestamp(time. ... BackData(eachStock) It shows me the following error main loop 'builtin_function_or_method' object is not iterable What is the problem please help me.
asked Apr 5, 2020 LizzyM 6.1k points