Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save billykong/951372f802a04a4578da878ae4721655 to your computer and use it in GitHub Desktop.
Save billykong/951372f802a04a4578da878ae4721655 to your computer and use it in GitHub Desktop.
Create dictionary from another dictionary with dictionary comprehension
cookie = {
'domain': 'pythonscraping.com',
'httponly': False,
'name': 'has_js',
'path': '/',
'secure': False,
'value': '1'
}
keys = ['name', 'value', 'domain', 'path', 'expiry'] # 'expiry' is absent from the cookie dict
new_cookie = {k: cookie[k] for k in keys if k in cookie.keys()}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment