0

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.

Daniela
  • 861
  • 5
  • 11
  • 28
  • 4
    because dictionaries are unordered - try using [`OrderedDict`](https://docs.python.org/3/library/collections.html#collections.OrderedDict) instead. – Phydeaux Mar 05 '19 at 15:26
  • 2
    @Phydeaux [before version 3.6 *](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6) – meowgoesthedog Mar 05 '19 at 15:26
  • @Daniela you can also just go `for live_departure in live_departures_data:`, you don't need to use the ranges and indexes. If you DO need the index, you should use `enumerate()`. – Soviut Mar 05 '19 at 15:30
  • @Soviut Thanks I didn't know about `enumerate()` it looks like it's what I need – Daniela Mar 05 '19 at 15:32
  • @Daniela Your example doesn't need to enumerate at all. You can nest 2 `for in` loops. – Soviut Mar 05 '19 at 16:09

1 Answers1

4

Dictionaries are defined as unordered key-value pairs. So, you can't rely on their ordering. If you want the order to be stable, use collections.OrderedDict

blue_note
  • 27,712
  • 9
  • 72
  • 90