Comprehension and generator expressions in python

#performance #code #python

  • List comprehension is a way to create list using the following syntax:
    • codes = [ord(x) for x in x]
  • List comprehension is fast than using a for loop to iterate a list
  • Generator expressions use () for to initialize
    • for tshirt in (f'{c} {s}' for c in colors for s in sizes): print(tshirt)
    • Code above does not generate a list neither occupy space in memory. Each item is generated by the time

Comprehension for dictionaries

>>> country_dial = {country: code for code, country in dial_codes}
>>> country_dial
{'Bangladesh': 880, 'Brazil': 55, 'China': 86, 'India': 91, 'Indonesia': 62,
'Japan': 81, 'Nigeria': 234, 'Pakistan': 92, 'Russia': 7, 'United States': 1}

Eu com algumas figuras que representa análise de dados/ciência
#performance #code #python