简体   繁体   中英

ValueError: too many values to unpack (expected 2) while iterating over a list

I have a list:

imageList = [(cloud/image_stored_docker:1.1.0, tarfile-1.1.0.tar),
             (cloud/image_stored_docker:1.2.0, tarfile-1.2.0.tar)]

I need to split in two lists, one list with keys (where images are keys) and other one with values (tarfiles).

I am trying to create a string and then convert it to list

for key, value  in imageList.items():
    images = images + key + ","

images = list(images)

but I get this message:

for key, value  in imageList.items():
AttributeError: 'list' object has no attribute 'items'

I tried other way; removing items() function and this is the message:

for key, value  in imageList:
ValueError: too many values to unpack (expected 2)

Removed fluff at end.\

if you just want to print a list of the keys you can use a list comprehension and iterate over the tuples

imageList = [("cloud/image_stored_docker:1.1.0", "tarfile-1.1.0.tar"), ("cloud/image_stored_docker:1.2.0", "tarfile-1.2.0.tar") ]
keys = [pair[0] for pair in  imageList]
print(",".join(keys))

OUTPUT

cloud/image_stored_docker:1.1.0,cloud/image_stored_docker:1.2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM