I’d like to turn the following list in a dictionary:
JavaScript
x
2
1
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]
2
I’d like to add the following headers. Each element of the list needs to have its own header.
JavaScript
1
2
1
detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']
2
This is my code:
JavaScript
1
9
1
num = len(detail_flights)
2
num_detail_headers = detail_headers*num
3
4
from itertools import zip_longest
5
new_detail_flights = detail_flights[0].split("n")
6
7
dictionary = dict(zip(num_detail_headers , new_detail_flights ))
8
print(dictionary)
9
And the following is my output. I obtain the dictionary only for the first element of the list and I can’t understand why.
JavaScript
1
2
1
{'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'}
2
Advertisement
Answer
the line
JavaScript
1
2
1
new_detail_flights = detail_flights[0].split("n")
2
only take the first element.
To take all element, you can do :
JavaScript
1
16
16
1
from itertools import zip_longest
2
3
4
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',
5
'17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn11:35 – 13:05nBCN Barcellona-El Pratn‐nMXP Aeroporto Malpensandiretton1h 30m',
6
'17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m',
7
'17:20 – 19:00nMXP Aeroporto Malpensan‐nBCN Barcellona-El Pratndiretton1h 40mn20:15 – 21:55nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m']
8
detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']
9
10
11
new_detail_flights = []
12
for flight in detail_flights:
13
new_detail_flights.append(dict(zip(detail_headers , flight.split("n"))))
14
15
print(new_detail_flights)
16
witch output :
JavaScript
1
37
37
1
[
2
{
3
'departure_time': '17:20 – 19:00',
4
'departure_airport': 'MXP Aeroporto Malpensa',
5
'to_delete': '‐',
6
'arrival_airport': 'BCN Barcellona-El Prat',
7
'type_flight': 'diretto',
8
'duration': '1h 40m'
9
},
10
{
11
'departure_time': '17:20 – 19:00',
12
'departure_airport': 'MXP Aeroporto Malpensa',
13
'to_delete': '‐',
14
'arrival_airport': 'BCN Barcellona-El Prat',
15
'type_flight': 'diretto',
16
'duration': '1h 40m'
17
},
18
{
19
'departure_time': '17:20 – 19:00',
20
'departure_airport': 'MXP Aeroporto Malpensa',
21
'to_delete': '‐',
22
'arrival_airport': 'BCN Barcellona-El Prat',
23
'type_flight': 'diretto',
24
'duration': '1h 40m'
25
},
26
{
27
'departure_time': '17:20 – 19:00',
28
'departure_airport': 'MXP Aeroporto Malpensa',
29
'to_delete': '‐',
30
'arrival_airport': 'BCN Barcellona-El Prat',
31
'type_flight': 'diretto',
32
'duration': '1h 40m'
33
}
34
]
35
36
37