{"id":3500,"date":"2023-10-22T15:53:13","date_gmt":"2023-10-22T10:23:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=3500"},"modified":"2023-11-10T11:54:40","modified_gmt":"2023-11-10T06:24:40","slug":"python-different-ways-to-create-dictionaries","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-different-ways-to-create-dictionaries\/","title":{"rendered":"Python : 6 Different ways to create Dictionaries"},"content":{"rendered":"
Dictionary in python is one of the important datatype which is used to store data values in Syntax of dictionary :<\/strong><\/p>\n where,<\/p>\n By 2 ways we can create empty dictionary.<\/p>\n Let’s see the implementation of it with a program.<\/p>\n Let’s see the implementation of it with a program.<\/p>\n By passing Let’s see the implementation of it with a program.<\/p>\n By creating dictionary constructor and passing \u00a0key-value pairs within it also we can create a dictionary.<\/p>\n Let’s see the implementation of it with a program.<\/p>\n We can create a dictionary by passing\u00a0 list of tuple within dict constructor.<\/p>\n Let’s see the implementation of it with a program.<\/p>\n We can create a dictionaries by assigning same vales to all the keys.<\/p>\n Suppose we a dictionary of keys. Let’s see how we can initialize all keys with same value.<\/p>\n If we have two lists, then also we can create a dictionary. For that we can use the elements of the first list as keys and we can use the elements of the second list as values.<\/p>\n In python there is a Let’s see the implementation of it with a program.<\/p>\n How we can create dictionaries by multiple ways in python ? Dictionary in python is one of the important datatype which is used to store data values in key : value pair. Syntax of dictionary : dictionary_name = {key1: value1, key2: value2} where, key1, key2… represents keys in a dictionary. These keys can be of …<\/p>\nkey : value<\/code> pair.<\/p>\n
dictionary_name = {key1: value1, key2: value2}<\/pre>\n
\n
Approach-1: Creating Empty Dictionary :<\/h3>\n
Method-1 : By using { } empty brackets :<\/h4>\n
#Program :\r\n\r\n# Empty dictionary created using empty brackets\r\ncovid_case_dict = {}\r\nprint(covid_case_dict)<\/pre>\n
Output :\r\n{ }<\/pre>\n
Method-2: By using using dict() :<\/h4>\n
#Program :\r\n\r\n# Empty dictionary created using dict()\r\ncovid_case_dict = dict()\r\nprint(covid_case_dict)<\/pre>\n
Output :\r\n{ }<\/pre>\n
Approach-2: Creating Dictionaries with literals :<\/h3>\n
key : value<\/code> pairs literals we can create dictionary.<\/p>\n
#Program :\r\n\r\n# dictionary created using key-value literals\r\ncovid_case_dict = {\"January\":100000, \"February\":200000, \"March\":300000}\r\nprint(covid_case_dict)<\/pre>\n
Output :\r\n{\"January\":100000, \"February\":200000, \"March\":300000}<\/pre>\n
Approach-3: Creating Dictionaries by passing parameters in dict constructor :<\/h3>\n
#Program :\r\n\r\n#dictionary created using dictionary constructor\r\ncovid_case_dict = dict(January=100000, February=200000, March=300000)\r\nprint(covid_case_dict)<\/pre>\n
Output :\r\n{\"January\":100000, \"February\":200000, \"March\":300000}<\/pre>\n
Approach-4: Creating Dictionaries by a list of tuples :<\/h3>\n
#Program :\r\n\r\n# list of tuples\r\nlist_of_Tuples = [(\"January\",100000), (\"February\",200000), (\"March\",300000)]\r\n#dictionary created by passing tuple in dict constructor\r\ncovid_case_dict = dict(list_of_Tuples)\r\nprint(covid_case_dict)<\/pre>\n
Output :\r\n{\"January\":100000, \"February\":200000, \"March\":300000}<\/pre>\n
Approach-5: Creating a Dictionary by a list of keys and initializing all with the same value :<\/h3>\n
#Program :\r\n\r\n# list of keys \r\ncovid_case_list = [\"Januray\", \"February\", \"March\"]\r\n# create and Initialize a dictionary \r\n#use list elements as keys and with same value 100000\r\ncovid_case_dict = dict.fromkeys(covid_case_list,100000 )\r\nprint(covid_case_dict)<\/pre>\n
Output :\r\n{\"January\":100000, \"February\":100000, \"March\":100000}<\/pre>\n
Approach-6: Creating a Dictionary by two lists :<\/h3>\n
zip( ) function<\/code> which we will be used and it will iterate over two lists parallelly.<\/p>\n
#Program :\r\n\r\n# First list, its elements will be used as keys \r\ncovid_case_list1 = [\"Januray\", \"February\", \"March\"]\r\n\r\n# Second list, its elements will be used as values\r\ncovid_case_list2 = [100000, 200000, 300000]\r\n#two lists are merged using zip() to create dictionary\r\ncovid_case_dict = dict( zip(covid_case_list1,covid_case_list2))\r\nprint(covid_case_dict)<\/pre>\n
Output : \r\n{\"January\":100000, \"February\":100000, \"March\":100000}<\/pre>\n","protected":false},"excerpt":{"rendered":"