{"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":"

Finding first key-value pair from a Dictionary in Python<\/h2>\n

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

Getting first key-value pair of dictionary in python using iter() & next() :<\/h3>\n

In python, iter()<\/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

In python, 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

Here from similar process, we will create a list of key-value pairs from dictionary. We can get first ‘N’ items by 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

We can slice first ‘N’ entries from a sequence by 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":"

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>\n

Get first key-value pair from a Python Dictionary<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nGet first key-value pair from a Python Dictionary - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get first key-value pair from a Python Dictionary - Python Programs\" \/>\n<meta property=\"og:description\" content=\"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 … Get first key-value pair from a Python Dictionary Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-29T05:13:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:35:26+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#webpage\",\"url\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/\",\"name\":\"Get first key-value pair from a Python Dictionary - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-29T05:13:23+00:00\",\"dateModified\":\"2023-11-10T06:35:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get first key-value pair from a Python Dictionary\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Get first key-value pair from a Python Dictionary\",\"datePublished\":\"2023-10-29T05:13:23+00:00\",\"dateModified\":\"2023-11-10T06:35:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#webpage\"},\"wordCount\":213,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get first key-value pair from a Python Dictionary - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/","og_locale":"en_US","og_type":"article","og_title":"Get first key-value pair from a Python Dictionary - Python Programs","og_description":"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 … Get first key-value pair from a Python Dictionary Read More »","og_url":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-29T05:13:23+00:00","article_modified_time":"2023-11-10T06:35:26+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#webpage","url":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/","name":"Get first key-value pair from a Python Dictionary - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-29T05:13:23+00:00","dateModified":"2023-11-10T06:35:26+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Get first key-value pair from a Python Dictionary"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Get first key-value pair from a Python Dictionary","datePublished":"2023-10-29T05:13:23+00:00","dateModified":"2023-11-10T06:35:26+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#webpage"},"wordCount":213,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/get-first-key-value-pair-from-a-python-dictionary\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/6162"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=6162"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/6162\/revisions"}],"predecessor-version":[{"id":6167,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/6162\/revisions\/6167"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=6162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=6162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=6162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}