langs = [language for language in languages if language['name'] == name]
This is a fairly simple example of a Python technique and several other languages called list comprehension " (" comprehension of lists "in Spanish) and serves to filter and transform any" iterable ", ie data that can be traversed, such as lists, tuples, dictionaries, just to name the basics. Its syntax could be explained as:
[expresión de salida] [ciclo] [filtro]
The [filtro]
can be optional, the [expresión de salida]
can be an element, several or a transformation. In the network you will find a huge amount of examples, as a sample, let's see these cases:
lista = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
# obtengo lo número pares
print([e for e in lista if e % 2 == 0])
[2, 4, 6, 8]
# obtengo el cuadrado de cada elemento
print([e*e for e in lista])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
# creamos un diccionario enumerando cada elemento
print({"elemento_{0}".format(i):e for i,e in enumerate(lista, 1)})
{'elemento_1': 1, 'elemento_2': 2, 'elemento_3': 3, 'elemento_4': 4, 'elemento_5': 5, 'elemento_6': 6, 'elemento_7': 7, 'elemento_8': 8, 'elemento_9': 9}
Going back to your example:
langs = [language for language in languages if language['name'] == name]
The explanation is: a new list langs
is initialized with the elements of a list of dictionaries languajes
that meet the condition that name
== name. That is, we are basically "filtering" the original list into a new one.