This question already has an answer here:
- if/else in Python's list comprehension? 5 answers
I have just started learning how to code with Python 3. i want to write a code that identifies which strings in a list have a certain length and for example if they have a certain length for them to be lower cased and if they don't have that certain length in characters, for them to be uppercased. So this is the code I was trying to make it work:
Phrase = ['apple','orange','whatnot']
if len(e) == 6:
e.upper() for e in Phrase
else:
e.lower() for e in Phrase
but I obviously get a syntax error. I know that if write the following code:
[e.lower() for e in Phrase if len(e) > 6]
It works but I don't know how to add else and elif statements to that.
Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire