lundi 30 mars 2020

Comparing a list with a dictionary and producing new list. Length of new list is 1?

I am trying to produce a List B by comparing elements from List A with X elements in Dictionary A. The List B should produce all Y elements from the dictionary where X and List A match.

This is my code:

for i in range(0,Len): # Len is length of List A
    tu = tuple1[3*i:3*i+3]

    if tu in diction:
        for x,y in diction.items():
            if tu == x:
                protlist = [y]
                print(*protlist,end =" ") # This prints each y value in a linear fashion
                break
print(len(protlist))  

This code will superficially produce the correct list. However when I call for the length of the list, it is outputted as 1. I tried replacing protlist = [y] with protlist.append(y). This gave the correct length of the list and the wrong output.

I also tried using the join function instead with ''.join(y) but this also gave the incorrect length of the list.

How can I edit the code so that both the correct output and length of list is achieved? Thank you.

Aucun commentaire:

Enregistrer un commentaire