Ask Your Question

TypeError: 'int' object is not iterable

asked 2012-07-03 06:03:11 -0500

MaelstromYamato gravatar image


For a given matrix M (m by n) and vector V of size n, my goal is to do the following:

  1. Take the $\text{i}^{\text{th}}$ column of M and convert them as keywords for a dictionary and associate the keywords to the $\text{i}^{\text{th}}$ value of the vector V.
    1. I do (1) in MatVec(M,V) in the code below by creating a dictionary for each keyword and value. Finally I try to merge all the dictionaries.

Here is my code:

def DictionaryMerge(*args):
    import collections
    super_dict = collections.defaultdict(set)
    for d in args:
        for k, v in d.iteritmes():
    return super_dict

def MatVec(M,V):
    from sets import Set
    Vec = V.list()
    MatCols = M.ncols()
    D2 = {}
    for i in range(MatCols):
        ColumnVec = M.column(i).list()
        CVec = list()
        for k in range(len(ColumnVec)):
        for j in CVec:
            dict = {j:Set(int(Vec[i]))}
            D2 = DictionaryMerge(dict,D2)
     return dict

M = matrix([[(i+1)*(j+2) for i in range(5)] for j in range(6)])
V = vector([int(i+100) for i in range(5)])

The error I get is:

Traceback (most recent call last):        return super_dict
  File "", line 1, in <module>

  File "/tmp/tmp4m6HiR/", line 29, in <module>
     exec compile(u'MatVec(M,V)
  File "", line 1, in <module>

  File "/tmp/tmp4m6HiR/", line 22, in MatVec
     dict = {j:Set(int(Vec[i]))}
  File "/home/usr111/sage/local/lib/python/", line 414, in __init__
  File "/home/usr111/sage/local/lib/python/", line 368, in _update
     for element in iterable:
 TypeError: 'int' object is not iterable

I am not so sure why I am getting this error.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-07-03 07:04:42 -0500

benjaminfjones gravatar image

The problem is in the line

dict = {j:Set(int(Vec[i]))}

The Set constructor takes an iterable object and the python int type is not iterable. If you change the line to:

dict = {j:Set( [int(Vec[i])] )}

that problem should go away.

edit flag offensive delete link more


Oh boy... I feel silly how that stumped me all day long. Thank you!

MaelstromYamato gravatar imageMaelstromYamato ( 2012-07-03 11:19:51 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2012-07-03 06:03:11 -0500

Seen: 4,924 times

Last updated: Jul 03 '12