fork download
  1. import re
  2.  
  3. lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n']
  4.  
  5. pattern = re.compile(r"(.+) \[(.+)\]\n?")
  6.  
  7. frutas = []
  8. cores = []
  9.  
  10. for item in lista:
  11. if pattern.match(item):
  12. fruta, cor = pattern.search(item).groups()
  13. frutas.append(fruta)
  14. cores.append(cor)
  15.  
  16. print(frutas)
  17. print(cores)
Success #stdin #stdout 0.02s 28384KB
stdin
Standard input is empty
stdout
['banana', 'uva', 'laranja', 'kiwi', 'framboesa', 'coco']
['amarela', 'vinho', 'laranjado', 'verde', 'vermelho', 'marrom']