TypeError: zip argument #1 must support iteration

Ting Ping picture Ting Ping · Dec 27, 2012 · Viewed 78.2k times · Source
for k,v in targets.iteritems():
    price= str(v['stockprice'])

    Bids = str(u''.join(v['OtherBids']))
    Bids = Bids.split(',')

    # create a list of unique bids by ranking
    for a, b in zip(float(price), Bids):
        try:
            del b[next(i for i, e in enumerate(b) if format(e, '.4f')  == a)]
        except StopIteration:
            pass

I am extracting data from my dictionary but it seems that all of them are in unicode. How may I get rid of the unicode nonsense?

Answer

Jim DeLaHunt picture Jim DeLaHunt · Dec 27, 2012

I take it your code is giving you the error message, TypeError: zip argument #1 must support iteration. You are getting this error due to the expression zip(float(price), Bids). This simplified code demonstrates the error:

>>> price = str(u'12.3456')
>>> bids = ['1.0', '2.0']
>>> zip(float(price), bids)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zip argument #1 must support iteration

The Python 2.x zip() built-in library function requires all its arguments to be iterables. float(price) is not an iterable.

If you want to make tuples combining float(price) with each element of the array Bids, you can use itertools.repeat() in the first argument expression.

>>> import itertools
>>> price = str(u'12.3456')
>>> bids = ['1.0', '2.0']
>>> zip(itertools.repeat(float(price),len(bids)), bids)
[(12.345599999999999, '1.0'), (12.345599999999999, '2.0')]

I don't see that your use of Unicode data types has anything to do with the TypeError.