How to Make a List of Dictionaries in Python

The Python programming language offers users a variety of useful built-in data types, including many which represent collections of data. You can use the most fundamental of these, the list, to organize a variety of data types under the same variable name, including other lists and collections of data. For example, you can create a series of dictionaries, which contain values and referencing keys, and store them as elements of a list.

Things You'll Need

  • Python Interpreter
Show More

Instructions

    • 1

      Create a list that will hold the dictionaries:

      >>>dictionaries = list()

    • 2

      Create two dictionary items. A dictionary is simply a list that contains a series of key-value pairs. A key-value pair is a value and a name for that value:

      >>>x = {'firstname' : 'Bob', 'lastname' : 'Smith'}
      >>>y = {'firstname' : 'Mark', 'lastname' : 'Johnson'}

    • 3

      Append the dictionary items to the list:

      >>>dictionaries.append(x)
      >>>dictionaries.append(x)

Related Searches:

References

Comments

Related Ads

Featured