{"id":6162,"date":"2023-10-29T10:43:23","date_gmt":"2023-10-29T05:13:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=6162"},"modified":"2023-11-10T12:05:26","modified_gmt":"2023-11-10T06:35:26","slug":"get-first-key-value-pair-from-a-python-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/","title":{"rendered":"Get first key-value pair from a Python Dictionary"},"content":{"rendered":"
In this article, we will see of different methods by which we can fetch the first key-value pair of a dictionary. We will also discuss how to get first N pairs and any number of key-value pairs from a dictionary.<\/p>\n
In python, In python, Here from similar process, we will create a list of key-value pairs from dictionary. We can get first ‘N’ items by We can slice first ‘N’ entries from a sequence by Finding first key-value pair from a Dictionary in Python In this article, we will see of different methods by which we can fetch the first key-value pair of a dictionary. We will also discuss how to get first N pairs and any number of key-value pairs from a dictionary. Getting first key-value pair of dictionary …<\/p>\niter()<\/code> function creates a iterator object of the the iterable sequence of key-value pairs from dictionary and by calling next() <\/em>function we can get the first key-value pair.<\/p>\n
# Program :\r\n\r\ndict_eg = {\r\n 'Sachin' : 10,\r\n \"Gayle\" : 333,\r\n 'Kohil' : 18,\r\n 'Murali' : 800,\r\n 'Dhoni' : 7,\r\n 'AB' : 17\r\n}\r\n# Get the first key-value pair in dictionary\r\ndict_eg = next(iter((dict_eg.items())) )\r\nprint('The first Key Value Pair in the Dictionary is:')\r\nprint(dict_eg)\r\nprint('First Key: ', dict_eg[0])\r\nprint('First Value: ', dict_eg[1])\r\n<\/pre>\n
Output :\r\nThe first Key Value Pair in the Dictionary is:\r\n('Sachin', 10)\r\nFirst Key:\u00a0 Sachin\r\nFirst Value:\u00a0 10<\/pre>\n
Get first key-value pair of dictionary using list :<\/h3>\n
items()<\/code> function in dictionary returns the iterable sequence of all key-value pairs. Then by creating a list from all key-value pairs in dictionary and by selecting the first item we will get first key-value pair of dictionary.<\/p>\n
# Program :\r\n\r\ndict_eg = {\r\n 'Sachin' : 10,\r\n \"Gayle\" : 333,\r\n 'Kohil' : 18,\r\n 'Murali' : 800,\r\n 'Dhoni' : 7,\r\n 'AB' : 17\r\n}\r\n# Get the first key-value pair in dictionary\r\ndict_eg = list(dict_eg.items())[0]\r\nprint('First Key Value Pair of Dictionary:')\r\nprint(dict_eg)\r\nprint('Key: ', dict_eg[0])\r\nprint('Value: ', dict_eg[1])\r\n<\/pre>\n
Output :\r\nFirst Key Value Pair of Dictionary:\r\n('Sachin', 10)\r\nKey:\u00a0 Sachin\r\nValue:\u00a0 10<\/pre>\n
Getting the first N key-value pair of dictionary in python using list & slicing :<\/h3>\n
list[:N]<\/code> <\/em>or any items by
list[start:end]<\/code>.<\/p>\n
# Program :\r\n\r\ndict_eg = {\r\n 'Sachin' : 10,\r\n \"Gayle\" : 333,\r\n 'AB' : 17,\r\n 'Murali' : 800,\r\n 'Dhoni' : 7,\r\n 'AB' : 17,\r\n 'Kohil' : 18\r\n}\r\nn = 5\r\n# Get first 5 pairs of key-value pairs\r\nfirstN_pairs = list(dict_eg.items())[:n]\r\nprint('The first 5 Key Value Pairs of Dictionary are:')\r\nfor key,value in firstN_pairs:\r\n print(key, '::', value)\r\n<\/pre>\n
Output :\r\nThe first 5 Key Value Pairs of Dictionary are:\r\nDhoni :: 7\r\nKohil :: 18\r\nGayle :: 333\r\nSachin :: 10\r\nAB :: 17<\/pre>\n
Getting the first N key-value pair of dictionary in python using itertools :<\/h3>\n
itertools.islice(iterable, stop)<\/code> after creating key-value pairs sequence from items() function.<\/p>\n
# Program :\r\n\r\nimport itertools\r\ndict_eg = {\r\n 'Sachin' : 10,\r\n \"Gayle\" : 333,\r\n 'AB' : 17,\r\n 'Murali' : 800,\r\n 'Dhoni' : 7,\r\n 'AB' : 17,\r\n 'Kohil' : 18\r\n}\r\nn = 5\r\n# Get first 5 pairs of key-value pairs\r\nfirstN_pairs = itertools.islice(dict_eg.items(), n)\r\nprint('The first 5 Key Value Pairs of Dictionary are:')\r\nfor key,value in firstN_pairs:\r\n print(key, '::', value)\r\n<\/pre>\n
Output :\r\nThe first 5 Key Value Pairs of Dictionary are:\r\nMurali :: 800\r\nAB :: 17\r\nSachin :: 10\r\nDhoni :: 7\r\nKohil :: 18<\/pre>\n","protected":false},"excerpt":{"rendered":"