{"id":9034,"date":"2021-09-30T14:00:47","date_gmt":"2021-09-30T08:30:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=9034"},"modified":"2021-11-22T18:34:35","modified_gmt":"2021-11-22T13:04:35","slug":"python-program-to-map-two-lists-into-a-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-map-two-lists-into-a-dictionary\/","title":{"rendered":"Python Program to Map Two Lists into a Dictionary using Zip(), Append() Functions | How to Combine Two Lists into a Dictionary?"},"content":{"rendered":"
Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n Dictionaries in Python: <\/strong>In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or hash table (from Computer Science). Each key denotes a value and is separated by a colon (:).<\/p>\n Curly brackets are used to define a dictionary. The value to the left of the colon is known as the key, while the value to the right of the colon is known as the value. A comma separates each (key, value) pair.<\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n Dictionary Comprehension is a technique used to create a dictionary in Python. In this process, we will combine two sets of data either in lists or arrays. It is a simple approach for creating dictionaries and uses pointed brackets({}).<\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly. Dictionaries in Python: In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or …<\/p>\n\n
<\/a>Python Programming Examples on How to Map Two Lists into a Dictionary<\/h2>\n
Enter number of elements to be added to the dictionary = 8\r\nEnter the keys : \r\nEnter the key1 = hello\r\nEnter the key2 = this\r\nEnter the key3 = is\r\nEnter the key4 = btechgeeks\r\nEnter the key5 = online\r\nEnter the key6 = coding\r\nEnter the key7 = python\r\nEnter the key8 = platform\r\nEnter the Values : \r\nEnter the value1 = 45\r\nEnter the value2 = 82\r\nEnter the value3 = 125\r\nEnter the value4 = 962\r\nEnter the value5 = 712\r\nEnter the value6 = 100\r\nEnter the value7 = 852\r\nEnter the value8 = 965<\/pre>\n
The resultant dictionary with keys = ['hello', 'this', 'is', 'btechgeeks', 'online', 'coding', 'python', 'platform'] \r\nvalues = [45, 82, 125, 962, 712, 100, 852, 965] :\r\n{'hello': 45, 'this': 82, 'is': 125, 'btechgeeks': 962, 'online': 712, 'coding': 100, 'python': 852, 'platform': 965}<\/pre>\n
Enter number of elements to be added to the dictionary = 5\r\nEnter the keys : \r\nEnter the key1 = good\r\nEnter the key2 = morning\r\nEnter the key3 = python\r\nEnter the key4 = script\r\nEnter the key5 = code\r\nEnter the Values : \r\nEnter the value1 = 745\r\nEnter the value2 = 123\r\nEnter the value3 = 985\r\nEnter the value4 = 100\r\nEnter the value5 = 585<\/pre>\n
The resultant dictionary with keys = ['good', 'morning', 'python', 'script', 'code'] \r\nvalues = [745, 123, 985, 100, 585] :\r\n{'good': 745, 'morning': 123, 'python': 985, 'script': 100, 'code': 585}<\/pre>\n
<\/a>Python Program to Map Two Lists into a Dictionary<\/h2>\n
Mapping Two Lists into a Dictionary using zip() ,append() Functions<\/h3>\n
\n
<\/a>Write a Python Program to Map Two Lists into a Dictionary?<\/h3>\n
# Declare two empty lists one to store keys and the other to store values.\r\nkeyslist = []\r\nvalueslist = []\r\n# Scan the number of elements to be added to the dictionary and store it in a variable.\r\nnumb = int(input(\"Enter number of elements to be added to the dictionary = \"))\r\nprint(\"Enter the keys : \")\r\nfor p in range(numb):\r\n # scanning the keys\r\n keyelement = input(\"Enter the key\"+str(p+1)+\" = \")\r\n keyslist.append(keyelement)\r\nprint(\"Enter the Values : \")\r\nfor p in range(numb):\r\n # scanning the values\r\n valueelement = int(input(\"Enter the value\"+str(p+1)+\" = \"))\r\n valueslist.append(valueelement)\r\n# Zip the two lists together and use dict() to turn them into a dictionary.\r\nresultdict = dict(zip(keyslist, valueslist))\r\n# printing the dictionary\r\nprint(\"The resultant dictionary with keys =\",\r\n keyslist, \"values =\", valueslist, \":\")\r\nprint(resultdict)\r\n<\/pre>\n
Enter number of elements to be added to the dictionary = 8\r\nEnter the keys : \r\nEnter the key1 = hello\r\nEnter the key2 = this\r\nEnter the key3 = is\r\nEnter the key4 = btechgeeks\r\nEnter the key5 = online\r\nEnter the key6 = coding\r\nEnter the key7 = python\r\nEnter the key8 = platform\r\nEnter the Values : \r\nEnter the value1 = 45\r\nEnter the value2 = 82\r\nEnter the value3 = 125\r\nEnter the value4 = 962\r\nEnter the value5 = 712\r\nEnter the value6 = 100\r\nEnter the value7 = 852\r\nEnter the value8 = 965\r\nThe resultant dictionary with keys = ['hello', 'this', 'is', 'btechgeeks', 'online', 'coding', 'python', 'platform'] values = [45, 82, 125, 962, 712, 100, 852, 965] :\r\n{'hello': 45, 'this': 82, 'is': 125, 'btechgeeks': 962, 'online': 712, 'coding': 100, 'python': 852, 'platform': 965}<\/pre>\n
<\/a>How to Combine Two Lists into a Dictionary Python?<\/h3>\n
\n
<\/a>Python Program to Map Two lists into a Dictionary using Dictionary Comprehension<\/h3>\n
items = ['book', 'pen', 'paper']\r\nquantities = [5, 10, 25]\r\nitems_dict = {key:value for key, value in zip(items, quantities)}\r\n\r\nprint(items_dict)<\/pre>\n
{'book': 5, 'pen': 10, 'paper': 25}<\/pre>\n
<\/a>How to Combine Two List and Convert into Dictionary using For Loop?<\/h3>\n
fruits = ['Orange', 'Pomegranate', 'Banana','Guava']\r\nprice = [80, 70, 112, 66]\r\n\r\nfruits_price = zip(fruits, price)\r\n\r\n# create dictionary\r\nfruits_dict = {}\r\n\r\nfor key, value in fruits_price:\r\nif key in fruits_dict:\r\n# handling duplicate keys\r\npass\r\nelse:\r\nfruits_dict[key] = value\r\n\r\nprint(fruits_dict)<\/pre>\n
{'Orange': 80, 'Pomegranate': 70, 'Banana': 112, 'Guava': 66}<\/pre>\n
\n
\n