.zip()

Hello,

I have 2 lists:

file_path_list = ['E:/wip/warp/sources/IMG_7882.JPG', 'E:/wip/warp/sources/IMG_7886.JPG']
fileName_noExtension = ['IMG_7882', 'IMG_7886']

list_zip = zip( fileName_noExtension, file_path_list)
    print(list_zip)

I have a strange result:

[('I', 'E:/wip/warp/sources/IMG_7882.JPG'), ('M', 'E:/wip/warp/sources/IMG_7886.JPG')]

Why the .zip() function does select only the first character of the fileName_noExtension’s elements?

Zip object is different from list object. Try using the list() method, I put an example below and tested it in the terminal.

file_path_list = ['E:/wip/warp/sources/IMG_7882.JPG', 'E:/wip/warp/sources/IMG_7886.JPG']
fileName_noExtension = ['IMG_7882', 'IMG_7886']
list_zip = zip(fileName_noExtension, file_path_list)

test = list(list_zip)
print(test)
# output: [('IMG_7882', 'E:/wip/warp/sources/IMG_7882.JPG'), ('IMG_7886', 'E:/wip/warp/sources/IMG_7886.JPG')]

Here’s some specific documentation on the topic:
https://docs.python.org/3/library/functions.html#zip

2 Likes

Thanks a lot for this light speed answer!

Yeah it works fine in my code editor application (Atom), but for some reason it doesn’t work in the application where I used thoses lines (Nuke), I have to investigate it.

Ahh I don’t know anything about nuke. My only comment is you may want to check the version of python that is expected to be compiled. But I don’t know if that comment is useful at all.

The only change I know of would be that zip used to return a list rather than an zip object. That would explain why the original print showed a list. I can’t work out where it would cut off characters though unless the fileName_noExtension variable only had one item and therefore it treated the string like an iterable (hence element 1 is ‘I’ and element 2 is ‘M’). That shouldn’t happen as it’s written though.

1 Like

I could see it being a thing if it’s not using a standard compiler?? Is that a thing? I feel like I read about it at some point.

I’m unfamiliar with Nuke myself but I did have to use Python2 for a long time so I recognise a fair few of the differences. That and an annoying level of OS changes so I’m always afraid of what version of a a program currently exists on the path etc. It wouldn’t surprise me for an instant if they just used different versions.

Yes it looks like something goes wrong when the code is compile since if I run thoses lines in the python script editor of Nuke, the result is fine…

EDIT
So I tried the entire code in the nuke script editor not only the .zip() part…and the result this time doesn’t works. So the problem is maybe more on my side with my code I think, but I checked it again and I can’t find a issue, maybe you could see something bad in this code?

sources_class = ["Read", "Camera2", "ReadGeo2"]
return_list = []
file_path_list = []
fileName_noExtension_list =[]

for node in nuke.allNodes():
        if node.Class() in sources_class:   
            return_list.append(node.name())
print(return_list)
for node in return_list: 
        current_node = nuke.toNode(node)
        filepath = current_node["file"].value()
        file_path_list.append(filepath)
        filename = os.path.basename(filepath)
        fileName_noExtension = filename[:filename.find(".")]
        fileName_noExtension_list.append(fileName_noExtension)
print(file_path_list)
print(fileName_noExtension_list)
    
zipped = zip(file_path_list,fileName_noExtension)
print(list(zipped))

So, I found my error, I called the wrong variable in my zip(fileName_noExtension instead of fileName_noExtension_list)…

Thanks for the help!

SOLVED

1 Like