{"id":3768,"date":"2023-10-24T16:19:30","date_gmt":"2023-10-24T10:49:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=3768"},"modified":"2023-11-10T11:57:29","modified_gmt":"2023-11-10T06:27:29","slug":"python-iterate-over-dictionary-with-index","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-iterate-over-dictionary-with-index\/","title":{"rendered":"Python: Iterate Over Dictionary with Index"},"content":{"rendered":"
Dictionaries are the implementation by Python of\u00a0a knowledge\u00a0<\/span>structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair. Examples:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n When working with iterators, we frequently encounter the need to keep track of the number of iterations. Python makes it easier for programmers by providing a built-in function enumerate() for this purpose. Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Return:<\/strong><\/p>\n We can traverse over the dictionary by passing given dictionary as parameter in enumerate() function<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The dictionary class’s keys() function returns an iterable sequence of all the dictionary’s keys. We can pass that to the enumerate() function, which will return keys as well as index position.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The dictionary class’s values() function returns an iterable sequence of all the dictionary’s values. We can pass that to the enumerate() function, which will return values as well as index position.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are the implementation by Python of\u00a0a knowledge\u00a0structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair. The list of key value pairs in curly braces\u00a0that’s\u00a0separated by comma defines a dictionary. Column \u2018:\u2019 separates the value of each key. A dictionary\u00a0can’t …<\/p>\n
\nThe list of key value pairs in curly braces\u00a0that’s\u00a0<\/span>separated by comma defines a dictionary. Column \u2018:\u2019 separates the value of each key.
\nA dictionary\u00a0can’t be\u00a0<\/span>sorted only\u00a0to urge\u00a0<\/span>a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore,\u00a0you would like\u00a0<\/span>an ordered data type, which\u00a0may be a\u00a0<\/span>list\u2014probably\u00a0an inventory\u00a0<\/span>of tuples.<\/p>\ndictionary = {'This': 100, 'is':200, 'BTechGeeks':300}<\/pre>\n
index = 0 ; key = this ; Value = 200\r\nindex = 1 ; key = is ; Value = 100\r\nindex = 2 ; key = BTechGeeks ; Value = 300<\/pre>\n
Traverse the Dictionary with Index<\/h2>\n
\n
1)Enumerate() function:<\/h3>\n
\nEnumerate() adds a counter to an iterable and returns it as an enumerate object. This enumerate object can then be\u00a0utilized in\u00a0<\/span>for loops directly or converted into\u00a0an inventory\u00a0<\/span>of tuples using the list() method.<\/p>\nenumerate(iterable, start=0)<\/pre>\n
iterable: an iterator, a sequence, or objects that support iteration\r\n\r\nstart : the index value from which the counter will be started; the default value is 0.<\/pre>\n
The method enumerate() adds a counter to an iterable and returns it. The object returned is an enumerate object.<\/pre>\n
2)Traverse over all key-value pairs of given dictionary by index<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# Traverse all key-value pairs of given dictionary by index\r\nfor i, key in enumerate(dictionary):\r\n print('index =', i, ' ; key =', key, ' ; Value =', dictionary[key])\r\n<\/pre>\n
index = 0 ; key = this ; Value = 200\r\nindex = 1 ; key = is ; Value = 100\r\nindex = 2 ; key = BTechGeeks ; Value = 300<\/pre>\n
3)Traverse over all keys of given dictionary by index<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# Traverse all keys of given dictionary by index\r\nfor i, key in enumerate(dictionary.keys()):\r\n print('index =', i, ' ; key =', key)\r\n<\/pre>\n
index = 0 ; key = this\r\nindex = 1 ; key = is\r\nindex = 2 ; key = BTechGeeks<\/pre>\n
4)Traverse over all values of given dictionary by index<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# Traverse all values of given dictionary by index\r\nfor i, value in enumerate(dictionary.values()):\r\n print('index =', i, ' ; value =', value)\r\n<\/pre>\n
index = 0 ; value = 200\r\nindex = 1 ; value = 100\r\nindex = 2 ; value = 300<\/pre>\n
\n