Arshad Mehmood

Search the blog

    Today I Learned

    Convert dictionary values or keys to list

    python

    In Python 3, the dict.keys() or dict.values() method returns a dictionary view object, which acts as a set. Iterating over the dictionary directly also yields keys, so turning a dictionary into a list results in a list of all the keys or values.

    orders = {
    	'cappuccino': 54,
    	'latte': 56,
    	'espresso': 72,
    	'americano': 48,
    	'cortado': 41
    }
    
    keys = list(orders.keys())
    values = list(orders.values())

    Source