How would I reference something akin to an input more than once with list comp in Python?
Solution 1:
One approach is to store it inside its own list and unpack it using for
string = input("String: ")
would become
for string in [input("String: ")]
>>> print([letter for string in [input("String: ")] for letter in string if letter == string[(len(string) - 1) // 2]])
String: abcde
['c']
formatted over multiple lines:
>>> print(
... [letter for string in [input("String: ")]
... for letter in string
... if letter == string[(len(string) - 1) // 2]]
... )
Also, your logic may have undesired behaviour.
String: abcdecccccc
['c', 'c', 'c', 'c', 'c', 'c', 'c']
Solution 2:
If I wanted to cram something like this onto one line I'd use a lambda
:
>>> print((lambda x: x[(len(x) - 1) // 2])(input()))
middle
d
In this case I think readability is vastly improved by doing the variable assignment on its own line, though.