Wondering how to work with If statements properly and assigning things with list comprehension

So, I'm currently working on this code to learn more about list comp:

lowercase = list(letter for letter in "abcdefghijklmnopqrstuvwxyz")
uppercase = list(letter for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")


print(
    list(
        alphabet[(len(alphabet) - 1) - index2]
        for index, letter in enumerate(str(input("Please input the text: ")))
        for index2, letter2 in enumerate(alphabet)
        if letter.islower() == True  # I want to make alphabet equal to the lowercase list here, with another statement doing the same if it's upper
        if letter == letter2
    )
)

The problem I'm finding here is making alphabet equal to the lowercase list if the letter in the first for loop ends up being lowercase - vice versa for uppercase. This isn't by any means a good code, but I'm looking to just learn for now.


Solution 1:

Here is the way you'd write a reversing cipher. Note that:

  1. You don't need to convert the alphabets to lists
  2. You don't need to call str() for an input; input always returns a string
  3. You don't need to enumerate the input text, because you never use the enumeration
  4. You don't need a loop to search the alphabet, you can use index
  5. You don't need the islower check, because you already check whether the letter is in the alphabet.
lowercase = "abcdefghijklmnopqrstuvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

alphabet = lowercase

print(
    list(
        alphabet[(len(alphabet) - 1) - alphabet.index(letter)]
        for letter in input("Please input the text: ")
        if letter in alphabet
    )
)

If you replace list with ''.join, it will print a string instead of a list.