Skip to content

Instantly share code, notes, and snippets.

@cemkaplan75
Created January 19, 2024 02:53
Show Gist options
  • Save cemkaplan75/88f84f39cd5f0588192143cfafd7c436 to your computer and use it in GitHub Desktop.
Save cemkaplan75/88f84f39cd5f0588192143cfafd7c436 to your computer and use it in GitHub Desktop.
# Data
energy_data = [
("Total", 96.9),
("Petroleum", 34.78),
("Natural gas", 26.59),
("Coal", 17.99),
("Nuclear", 8.33),
("Biofuels", 3.89),
("Hydroelectric", 2.47),
("Wind", 1.73),
("Waste", 0.47),
("Solar", 0.43),
("Geothermal", 0.22),
]
# Separate Total row
total_row = [entry for entry in energy_data if entry[0] == "Total"][0]
energy_data.remove(total_row)
# Sorting by Quadrillion BTUs in descending order
sorted_energy_data = sorted(energy_data, key=lambda x: x[1], reverse=True)
# Add Total row back at the end
sorted_energy_data.append(total_row)
# Print sorted data
for source, quadrillion_btu in sorted_energy_data:
print(f"{source}: {quadrillion_btu} Quadrillion BTUs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment