i’m working on something silly, it’s just a fun little encoding thing to mess around and get back in the spirit after being too busy to code for a while. i realised while writing it that the only strategy i’ve found to work for using dictionaries in this way is completely unpythonic, but i specifically remember scouring the internet while learning this method. i think i had to come up with this myself, because the easiest way i’d found otherwise was just to iterate and check through the dict.values() list, which seemed silly. i basically just want to be able to access not only values by key, but keys by value. i feel like making two dictionaries is just inefficient, but so is the iterate-and-check process. is there some better way that i’m missing? here’s an example snippet of some code:
def encode(plaintext, shift, alphabet=”ABCDEFGHIJKLMNOPQRSTUVWXYZ”): # plaintext:string; shift:int;:
alpha_dict = {old_let[old_pos] for old_let, old_pos in enumerate(alphabet)} # create a dict of letters in the original alphabet and their original positions
reverse_dict = {old_pos[old_let] for old_let, old_pos in enumerate(alphabet)} # create the exact same dict, but key-value switched, so that you can access letter based on position
this isn’t really an error, it’s just a thing i think is weird? i tried the two methods i mentioned before, i suppose. i expected something more pythonic to be possible, and i can’t find such.