Lists to dictionary

Python, List, Dictionary · Nov 2, 2020

Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values. The values of the first list need to be unique and hashable.

  • Use zip() in combination with dict() to combine the values of the two lists into a dictionary.
def to_dictionary(keys, values):
  return dict(zip(keys, values))
to_dictionary(['a', 'b'], [1, 2]) # { a: 1, b: 2 }

More like this

  • Map list to dictionary

    Maps the values of a list to a dictionary using a function.

    Python, List · Nov 2, 2020

  • Pluck values from list of dictionaries

    Converts a list of dictionaries into a list of values corresponding to the specified key.

    Python, List · Oct 22, 2020

  • Merge lists

    Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions.

    Python, List · Nov 2, 2020