Skip to content

Instantly share code, notes, and snippets.

@antonioccneto
Created February 16, 2022 19:46
Show Gist options
  • Save antonioccneto/02964bc2cff49a81f3b984adf5589bbc to your computer and use it in GitHub Desktop.
Save antonioccneto/02964bc2cff49a81f3b984adf5589bbc to your computer and use it in GitHub Desktop.
# Contar total de itens em dicionários em uma lista
nomes = []
total = []
usuario = {}
lista = [{'user': 'João', 'idade': 25}, {'user': 'Maria', 'idade': 22}, {'user': 'João', 'idade': 33}]
for item in lista:
   if not item.get('user') in nomes:
      nomes.append(item.get('user'))
for user in nomes:
   usuario = {'user': user, 'total': 0}
   for item in lista:
      if item.get('user')==user:
         usuario['total'] += 1
   total.append(usuario)
      
print(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment