Skip to content

Latest commit

 

History

History
55 lines (28 loc) · 742 Bytes

Populate_hash_with_array_keys_and_default_value.md

File metadata and controls

55 lines (28 loc) · 742 Bytes

CodeWars Python Solutions


Populate hash with array keys and default value

Complete the function so that it takes an array of keys and a default value and returns a hash (Ruby) / dictionary (Python) with all keys set to the default value.

Examples

["draft", "completed"], 0   # should return {"draft": 0, "completed: 0}

Given Code

def populate_dict(keys, default):
    pass

Solution 1

def populate_dict(keys, default):
    return {i:default for i in keys}

Solution 2

def populate_dict(keys, default):
    return dict.fromkeys(keys, default)

See on CodeWars.com