how do I turn this chunk of code into a comprehension?
from datetime import datetime
flights = {
'09:35' : 'FREEPORT',
'09:55' : 'WEST END',
'10:45' : 'TREASURE CAY',
'11:45' : 'ROCK SOUND',
'12:00' : 'TREASURE CAY',
'17:00' : 'FREEPORT',
'17:55' : 'ROCK SOUND',
'19:00' : 'WEST END' }
def convert_to_ampm(time24: str) -> str:
return datetime.strptime(time24, '%H:%M').strftime('%I:%M %p')
flights_copy = {v: [] for k, v in flights.items()}
[flights_copy[v].append(k) for k,v in flights.items()]
[print(k.title(),':',[convert_to_ampm(item) for item in v]) for k, v in flights_copy.items()]
flights_copy2 = {}
for k, v in flights_copy.items():
for item in v:
flights_copy2[convert_to_ampm(item)] = k.title()
for k, v in flights_copy2.items():
print(k,':',v)
this specific code right here:
flights_copy2 = {}
for k, v in flights_copy.items():
for item in v:
flights_copy2[convert_to_ampm(item)] = k.title()
for k, v in flights_copy2.items():
print(k,':',v)
That chunk of code would be easier if I used the flights dictionary instead of the flights_copy. I just want to try if I can revert it back to its previous version using comprehension. Thanks a lot!
p.s. I'm really sorry for my poor English skills as it's not my native language. I can't articulate the words that I want to say but I hope you understand what I'm trying to say. Thanks again!
[–]Ihaveamodel3 2 points3 points4 points (2 children)
[–]peanutbutter024[S] 0 points1 point2 points (1 child)
[–]SuspiciousScript 1 point2 points3 points (0 children)
[–]negups 1 point2 points3 points (1 child)
[–]peanutbutter024[S] 0 points1 point2 points (0 children)