[Solved] How do I optimize Python Code?


data = [['From', '[email protected]', 'Fri', 'Jan', '14', '22:16:24', '2012'],
     ['From', '[email protected]', 'Fri', 'Jan', '14', '23:16:24', '2012'],
     ['From', '[email protected]', 'Fri', 'Jan', '14', '21:16:24', '2012'],
     ['From', '[email protected]', 'Fri', 'Jan', '14', '22:02:24', '2012']
    ]

hour_frequency_list = {}

for temp in data:
  hour = temp[5].split(":")[0]
  if hour in hour_frequency_list:
     hour_frequency_list[hour] += 1
  else:
     hour_frequency_list[hour] = 1

sorted_list = sorted(hour_frequency_list.items())

print ("hour | Occurences")
for k in sorted_list:
    print (k[0] + "    |" + str(k[1]) )

Output

hour  | Occurences
21    |1
22    |2
23    |1

3

solved How do I optimize Python Code?