Solution 1

def longest_key(a_dict):
    longest = None
    for key in a_dict.keys():
        if not longest or len(key) > len(longest):
            longest = key
    return longest

Largest key

Write a function that receives a dictionary with only String keys and returns the longest key in the dictionary. Example:

longest_key({'hello': 'World', 'abc': 123})  # hello
longest_key({})  # None

Test Cases

test one item - Run Test

def test_one_item():
    assert longest_key({'rmotr': 'Intro to Python'}) == 'rmotr'

test with no items - Run Test

def test_with_no_items():
    assert longest_key({}) == None

test with many items - Run Test

def test_with_many_items():
    assert longest_key({'abcd': 'XXYYZZ', 'ac': 'MMM'}) == 'abcd'
def longest_key(a_dict): pass