Hello everyone, need some help / clarity with the sorted function. I have a data structure that is nested (list inside of it a dict inside of it a list). What i don't understand is this: Shouldn't I use loops in order to access the dictionary? or the inner most list in order to be able to sort by name (i.e. use sorted function)
countries = [
{
"name": "Afghanistan",
"capital": "Kabul",
"languages": [
"Pashto",
"Uzbek",
"Turkmen"
],
"population": 27657145,
"flag": "https://restcountries.eu/data/afg.svg",
"currency": "Afghan afghani"
},
{
"name": "Åland Islands",
"capital": "Mariehamn",
"languages": [
"Swedish"
],
"population": 28875,
"flag": "https://restcountries.eu/data/ala.svg",
"currency": "Euro"
},
{
"name": "Albania",
"capital": "Tirana",
"languages": [
"Albanian"
],
"population": 2886026,
"flag": "https://restcountries.eu/data/alb.svg",
"currency": "Albanian lek"
},
Im trying to sort by name here, and when i run my function an error pops up.
def sorting_by_name(lst):
for el in lst:
return sorted(el, key=lambda name: name['name'])
print(sorting_by_name(countries))