Skip to content
Advertisement

Turning a list into a dictionary adding headers

I’d like to turn the following list in a dictionary:

detail_flights = ['17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn6:20 – 8:00nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m', '17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn11:35 – 13:05nBCN Barcellona-El Pratn‐nMXP Aeroporto Malpensandiretton1h 30m', '17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m', '17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn20:15 – 21:55nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m]

I’d like to add the following headers. Each element of the list needs to have its own header.

detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']

This is my code:

num = len(detail_flights)
num_detail_headers = detail_headers*num

from itertools import zip_longest
new_detail_flights = detail_flights[0].split("n") 

dictionary = dict(zip(num_detail_headers , new_detail_flights ))
print(dictionary)

And the following is my output. I obtain the dictionary only for the first element of the list and I can’t understand why.

{'departure_time': '6:20 – 8:00', 'departure_airport': 'BCN Barcellona-El Prat', 'to_delete': '‐', 'arrival_airport': 'BGY Bergamo Orio al Serio', 'type_flight': 'diretto', 'duration': '1h 40m'}

Advertisement

Answer

the line

new_detail_flights = detail_flights[0].split("n") 

only take the first element.

To take all element, you can do :

from itertools import zip_longest


detail_flights = ['17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn6:20 – 8:00nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m',
                  '17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn11:35 – 13:05nBCN Barcellona-El Pratn‐nMXP Aeroporto Malpensandiretton1h 30m',
                  '17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m',
                  '17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn20:15 – 21:55nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m']
detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']


new_detail_flights = []
for flight in detail_flights:
    new_detail_flights.append(dict(zip(detail_headers , flight.split("n"))))

print(new_detail_flights)

witch output :

[
    {
        'departure_time': '17:20 – 19:00',
        'departure_airport': 'MXP Aeroporto Malpensa',
        'to_delete': '‐',
        'arrival_airport': 'BCN Barcellona-El Prat',
        'type_flight': 'diretto',
        'duration': '1h 40m'
    }, 
    {
        'departure_time': '17:20 – 19:00',
        'departure_airport': 'MXP Aeroporto Malpensa',
        'to_delete': '‐',
        'arrival_airport': 'BCN Barcellona-El Prat',
        'type_flight': 'diretto',
        'duration': '1h 40m'
    },
    {
        'departure_time': '17:20 – 19:00',
        'departure_airport': 'MXP Aeroporto Malpensa',
        'to_delete': '‐',
        'arrival_airport': 'BCN Barcellona-El Prat',
        'type_flight': 'diretto',
        'duration': '1h 40m'
    },
    {
        'departure_time': '17:20 – 19:00',
        'departure_airport': 'MXP Aeroporto Malpensa',
        'to_delete': '‐',
        'arrival_airport': 'BCN Barcellona-El Prat',
        'type_flight': 'diretto',
        'duration': '1h 40m'
    }
]


User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement