Skip to content

Latest commit

 

History

History
46 lines (26 loc) · 725 Bytes

Turn_String_Input_into_Hash.md

File metadata and controls

46 lines (26 loc) · 725 Bytes

CodeWars Python Solutions


Turn String Input into Hash

Please write a function that will take a string as input and return a hash. The string will be formatted as such. The key will always be a symbol and the value will always be an integer.

"a=1, b=2, c=3, d=4"

This string should return a hash that looks like

{ 'a': 1, 'b': 2, 'c': 3, 'd': 4}

Given Code

def str_to_hash(st):
    # your code here
    pass

Solution

def str_to_hash(st):
    return {i[0]:int(i[1]) for i in [item.split("=") for item in st.split(", ")]} if len(st) > 0 else {}

See on CodeWars.com