Simplicity
Simplicity

Reputation: 48946

Python - dictionary update sequence element #0 has length 40; 2 is required

I have written the following script:

import os

i = 0
images = []
images_classes = {}
directory = '/home/Desktop/Images'

for image in os.listdir(directory):
    images.append(image)
    dir_path = os.path.dirname(os.path.realpath(image[i]))
    dir_name = os.path.basename(dir_path)
    images_classes.update({image, dir_name})
    i = i + 1

print images_classes

When I try to run the program, I get the following error:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    images_classes.update({image, dir_name})
ValueError: dictionary update sequence element #0 has length 40; 2 is required

I'm basically trying to add new elements to the dictionary at each iteration. How can I do so?

Thanks.

Upvotes: 0

Views: 2386

Answers (1)

Bill Bell
Bill Bell

Reputation: 21663

The doc for dict says, 'update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).'

What you've passed is neither.

Upvotes: 1

Related Questions