slacklikehellking9
slacklikehellking9

Reputation: 23

Ways to convert for loop into single expression list line

I wish for convert my for loop that contains if and else into single list expression line.

for k,v in zip(str+letters, str+letterb):
  if v in "aeiou":
    d[k] = v.upper()
  else:
    d[k] = v.lower()

into one line. The output doesnt matter because my code is correct when its like this

Upvotes: 1

Views: 67

Answers (1)

pixie999
pixie999

Reputation: 498

You can use a dictionary comprehension

d = {k: (v.upper() if v in "aeiou" else v.lower()) for k,v in zip(str+letters, str+letterb)}

Upvotes: 1

Related Questions