Dictionary to list

Python, Dictionary, List · Nov 2, 2020

Converts a dictionary to a list of tuples.

  • Use dict.items() and list() to get a list of tuples from the given dictionary.
def dict_to_list(d):
  return list(d.items())

d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
dict_to_list(d)
# [('one', 1), ('three', 3), ('five', 5), ('two', 2), ('four', 4)]

More like this

  • Python Lists

    A snippet collection of list helpers and tips for Python 3.6.

    Collection · 100 snippets

  • Combine dictionary values

    Combines two or more dictionaries, creating a list of values for each key.

    Python, Dictionary · Apr 4, 2021

  • Get nested value

    Retrieves the value of the nested key indicated by the given selector list from a dictionary or list.

    Python, Dictionary · Oct 28, 2020

  • Dictionary keys

    Creates a flat list of all the keys in a flat dictionary.

    Python, Dictionary · Nov 2, 2020