Skip to content

Latest commit

 

History

History
65 lines (41 loc) · 1.21 KB

Dictionary_from_two_lists.md

File metadata and controls

65 lines (41 loc) · 1.21 KB

CodeWars Python Solutions


Dictionary from two lists

There are two lists, possibly of different lengths. The first one consists of keys, the second one consists of values. Write a function createDict(keys, values) that returns a dictionary created from keys and values. If there are not enough values, the rest of keys should have a None (JS null) value. If there not enough keys, just ignore the rest of values.

Example 1

keys = ['a', 'b', 'c', 'd']
values = [1, 2, 3]
createDict(keys, values) # returns {'a': 1, 'b': 2, 'c': 3, 'd': None}

Example 2

keys = ['a', 'b', 'c']
values = [1, 2, 3, 4]
createDict(keys, values) # returns {'a': 1, 'b': 2, 'c': 3}

Given Code

def createDict(keys, values):
    pass

Solution 1

def createDict(keys, values):
    d = {}
    for e,i in enumerate(keys):
        if e < len(values):
            d[i] = values[e]
        else:
            d[i] = None
    return d

Solution 2

def createDict(keys, values):
    return {k:(values[e] if e<len(values) else None) for e,i in enumerate(keys)}

See on CodeWars.com