Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Created July 3, 2018 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidpixo/8d257abb73d006ba73a169648cc6aef7 to your computer and use it in GitHub Desktop.
Save kidpixo/8d257abb73d006ba73a169648cc6aef7 to your computer and use it in GitHub Desktop.
extract_list_to_dict
# goal : extract a list of string based on first char.
# use case : parsing argument given to a cli command.
from itertools import groupby
arg_tags = ['!dog', 'dog', '!Monty'] # input values
{{1:'exclude',0:'include'}[k]: # key value from dict
list(g) # values from groupby
for k,g in groupby(
sorted(
arg_tags, # argument to sort
key=lambda i : i[0] # based on first char
) ,
lambda i : i[0] == '!' # groupby function function
)
}
# Out: {'exclude': ['!dog', '!Monty'], 'include': ['dog']}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment