I'm comparing a dictionary LIVE_DEPARTURES_DATA
of ferry departures to determine if they have the same DEPARTURE TIME
and DEPARTURE PORT
. As I loop through it and do the relevant checks I noticed the order of the items is changed.
live_departures_data = [{"COMPANY": "Alilauro", "DEPARTURE PORT": "Ischia", "ARRIVAL PORT": "Napoli Molo Beverello", "DEPARTURE DATE": "2019-03-03", "DEPARTURE TIME": "08:40", "ARRIVAL DATE": "2019-03-03", "ARRIVAL TIME": "09:30", "DURATION": "00:50:00", "FERRY TYPE": "Aliscafo", "STATUS": "Active"}, {"COMPANY": "Alilauro", "DEPARTURE PORT": "Procida", "ARRIVAL PORT": "Casamicciola", "DEPARTURE DATE": "2019-03-03", "DEPARTURE TIME": "08:40", "ARRIVAL DATE": "2019-03-03", "ARRIVAL TIME": "09:30", "DURATION": "00:50:00", "FERRY TYPE": "Aliscafo", "STATUS": "Active"}, {"COMPANY": "Alilauro", "DEPARTURE PORT": "Procida", "ARRIVAL PORT": "Napoli Molo Beverello", "DEPARTURE DATE": "2019-03-03", "DEPARTURE TIME": "08:40", "ARRIVAL DATE": "2019-03-03", "ARRIVAL TIME": "09:30", "DURATION": "00:50:00", "FERRY TYPE": "Aliscafo", "STATUS": "Active"}]
for i in range(len(live_departures_data)):
for j in range(i + 1, len(live_departures_data)):
if live_departures_data[i]["ARRIVAL PORT"] == live_departures_data[j]["ARRIVAL PORT"] and live_departures_data[i]["DEPARTURE TIME"] == live_departures_data[j]["DEPARTURE TIME"]:
live_departures_data[i].update({'STOPS': 'Via Procida'})
live_departures_data[j].update({'STOPS': 'Via Procida'})
print(live_departures_data)
The output is the following - why has the order changed?
[{'STATUS': 'Active', 'ARRIVAL DATE': '2019-03-03', 'DEPARTURE DATE': '2019-03-03', 'STOPS': 'Via Procida', 'DURATION': '00:50:00', 'ARRIVAL PORT': 'Napoli Molo Beverello', 'COMPANY': 'Alilauro', 'ARRIVAL TIME': '09:30', 'FERRY TYPE': 'Aliscafo', 'DEPARTURE TIME': '08:40', ...
UPDATE:
I've updated to Python 3.7 and run my code. My initial order is kept.