Get one key from dictionary python
Dec 23, 2010 · In case you expect the dictionary to contain None values, you can use some more esoteric constants like NotImplemented, Ellipsis or make a new one: MyConst = object () def update_key (A, B, key): value = B.get (key, MyConst) if value is not MyConst: A [key] = value. Anyway, using update () is the most readable option for me: Python has a set of built-in methods that you can use on dictionaries. Method. Description. clear () Removes all the elements from the dictionary. copy () Returns a copy of the dictionary. fromkeys () Returns a dictionary with the specified keys and value. Oct 20, 2012 · A solution to shorten the condition can be the following sample: dict = {} dict ['1'] = 10 dict ['1'] = dict.get ('1', 0) + 1 if '1' in dict else 1 print (dict) dict is the build-in name of dictionaries. The answer is functunally equivalent to the already accepted one with an unneccessary inline if function. Definition and Usage. The keys () method returns a view object. The view object contains the keys of the dictionary, as a list. The view object will reflect any changes done to the dictionary, see example below. Aug 3, 2022 · Adding to a Dictionary Using the = Assignment Operator. You can use the = assignment operator to add a new key to a dictionary: dict[key] = value. If a key already exists in the dictionary, then the assignment operator updates, or overwrites, the value. The following example demonstrates how to create a new dictionary and then use the ... Nov 7, 2013 · One way to solve this, is by changing your last line: print(key, len([item for item in value if item])) ... Count occurrences of each key in python dictionary. 4. mydictionary = {'a': 'apple', 'b': 'bear', 'c': 'castle'} keys = ['b', 'c'] values = list ( map (mydictionary.get, keys) ) # values = ['bear', 'castle'] I timed this solution compared to a list comprehension (which would have otherwise been my preference) and this solution without encasing in a list is 3.5x faster so (+1) from me for the cases ... Apr 21, 2016 · If you wanted to return a dictionary, you could use a dictionary comprehension instead of the list comprehension in Sven Marnach's answer like so: d = dict.fromkeys(range(100)) keys = random.sample(d.keys(), 10) sample_d = {k: d[k] for k in keys} May 27, 2023 · Creating a Dictionary. In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value. Sep 22, 2020 · Dictionaries are important data structures in Python that use keys for indexing. They are an unordered sequence of items (key-value pairs), which means the order is not preserved. The keys are immutable. Just like lists, the values of dictionaries can hold heterogeneous data i.e., integers, floats, strings, NaN, Booleans, lists, arrays, and ... Sep 28, 2015 · Here is how to turn each dictionary into a set we can use: set_1 = set (test_1.items ()) set_2 = set (test_2.items ()) This returns a set containing a series of tuples. Each tuple represents one key/value pair from your dictionary. Now, to find the difference between set_1 and set_2: 2. Probably the quickest way to retrieve only the key name: mydic = {} mydic ['key_name'] = 'value_name' print mydic.items () [0] [0] Result: key_name. Converts the dictionary into a list then it lists the first element which is the whole dict then it lists the first value of that element which is: key_name. Share. Jul 15, 2015 · A call to without_keys(my_dict, ['keyB', 'keyC']) should return: { "keyA": 1 } I would like to do this in a one-line with a neat dictionary comprehension but I'm having trouble. My attempt is this: def without_keys(d, keys): return {k: d[f] if k not in keys for f in d} which is invalid syntax. How can I do this? Method 1: - To get the keys using .keys() method and then convert it to list. some_dict = {1: 'one', 2: 'two', 3: 'three'} list_of_keys = list(some_dict.keys()) print(list_of_keys) -->[1,2,3] Method 2: - To create an empty list and then append keys to the list via a loop. Definition and Usage. The keys () method returns a view object. The view object contains the keys of the dictionary, as a list. The view object will reflect any changes done to the dictionary, see example below. 665 5 17. Add a comment. 3. Here are separate functions to get a key, value or item: import random def pick_random_key_from_dict (d: dict): """Grab a random key from a dictionary.""" keys = list (d.keys ()) random_key = random.choice (keys) return random_key def pick_random_item_from_dict (d: dict): """Grab a random item from a dictionary ... Jun 15, 2012 · I came across the dict method get which, given a key in the dictionary, returns the associated value. ... one might consider using dictionary.get ... Python 3.8 and ... Jun 21, 2009 · Add new keys to a nested dictionary. If a key has to be added to a dictionary that is nested inside a dictionary, dict.setdefault (or collections.defaultdict) is really useful. For example, let's try to add a new key-value pair to mydict only nested inside another key: 'address'. Get Dictionary Value By Key With Get() in Python. To get the value of the key you want, you have to use the get() function using Python. The function requires a single argument which is the key in the dictionary. The below example contains 6 elements with both keys and the associated value of the dictionary. Use the method given below to get ... Sep 8, 2016 · @ribamar the question is "Comparing two dictionaries [...]". The 'invalid dict' above with list keys is not valid python code - dict keys must be immutable. Therefore you are not comparing dictionaries. If you try and use a list as a dictionary key your code will not run. You have no objects for which to compare. Method 1: - To get the keys using .keys() method and then convert it to list. some_dict = {1: 'one', 2: 'two', 3: 'three'} list_of_keys = list(some_dict.keys()) print(list_of_keys) -->[1,2,3] Method 2: - To create an empty list and then append keys to the list via a loop. EVENT is a dictionary, that is correct. But the value corresponding to the key 'acknowledges' is a list, which you can see this from the brackets []. This list has only one entry which is a dictionary, denoted by the braces {}. So what you have is a dictionary in a list in a dictionary, which seemed strange to me. Feb 8, 2022 · In this Program, we will discuss how to get the multiple values for one key in Python dictionary. To do this task, we are going to select the specific key and it gives the value of that key. In this example, we will create a dictionary and then declare a variable ‘result’ in which we will assign the selected item from the dictionary. May 16, 2023 · Step-by-step approach: Create a list test_list containing dictionaries with key-value pairs. Use list comprehension to create a new list res. For each dictionary d in test_list, check if the key ‘gfg’ is in the dictionary. If the key ‘gfg’ is in the dictionary, get its value and append it to the list res. Apr 28, 2021 · The keys in a Python dictionary are already distinct from each other. You can't have the exact some keyword as a key twice in a Python dictionary. Therefore, counting the number of keys is the same as counting the number of distinct keys. Aug 7, 2020 · So, I made this dictionary in python that has my class timetable. So, I created a dictionary for each lesson. friday = {} friday.setdefault('1st', []).append(8.40) # ... Call list () on the dictionary instead: keys = list (test) In Python 3, the dict.keys () method returns a dictionary view object, which acts as a set. Iterating over the dictionary directly also yields keys, so turning a dictionary into a list results in a list of all the keys: >>> test = {'foo': 'bar', 'hello': 'world'} >>> list (test) ['foo ... Apr 27, 2023 · Python | Initialize a dictionary with only keys from a list; Python | Get key with maximum value in Dictionary; Python | Split dictionary keys and values into separate lists; Python | Sum list of dictionaries with same key; Python | Filter the negative values from given dictionary; Python program to find the highest 3 values in a dictionary Get Dictionary Value By Key With Get() in Python. To get the value of the key you want, you have to use the get() function using Python. The function requires a single argument which is the key in the dictionary. The below example contains 6 elements with both keys and the associated value of the dictionary. Use the method given below to get ... carolina country music festival
Nov 7, 2013 · One way to solve this, is by changing your last line: print(key, len([item for item in value if item])) ... Count occurrences of each key in python dictionary. 4. Jun 16, 2023 · The keys () method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python. Syntax: dict.keys () Parameters: There are no parameters. Returns: A view object is returned that displays all the keys. This view object changes according to the changes in the dictionary. In the above example, we have used the keys() method to extract the keys of the dictionary. The list of keys are returned as a view object. Here, dict_keys() is the view object and ['name', 'age', 'salary'] is the list of keys of the dictionary employee. Feb 8, 2015 · 1 Answer. Python dicts can have only one value for a key, so you cannot assign multiple values in the fashion you are trying to. Instead, store the mutiple values in a list corresponding to the key so that the list becomes the one value corresponding to the key: d = {} d ["a"] = [] d ["a"].append (1) d ["a"].append (2) >>> print d {'a': [1, 2 ... Sep 22, 2020 · Dictionaries are important data structures in Python that use keys for indexing. They are an unordered sequence of items (key-value pairs), which means the order is not preserved. The keys are immutable. Just like lists, the values of dictionaries can hold heterogeneous data i.e., integers, floats, strings, NaN, Booleans, lists, arrays, and ... Nov 7, 2013 · One way to solve this, is by changing your last line: print(key, len([item for item in value if item])) ... Count occurrences of each key in python dictionary. 4. W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more. To delete a key regardless of whether it is in the dictionary, use the two-argument form of dict.pop():. my_dict.pop('key', None) This will return my_dict[key] if key exists in the dictionary, and None otherwise. In the above example, we have used the keys() method to extract the keys of the dictionary. The list of keys are returned as a view object. Here, dict_keys() is the view object and ['name', 'age', 'salary'] is the list of keys of the dictionary employee. download from link
Jul 25, 2018 · The dict contains only one key value. I am trying to get key from this dict, I tried d.keys()[0] but it returns IndexError, I tried this: list(d.keys())[0] It works just fine but I think it is not a good way of doing this because it creates a new list and then get it first index. Is there a better way to do this? I want to get the value too. 2. Probably the quickest way to retrieve only the key name: mydic = {} mydic ['key_name'] = 'value_name' print mydic.items () [0] [0] Result: key_name. Converts the dictionary into a list then it lists the first element which is the whole dict then it lists the first value of that element which is: key_name. Share. Apr 21, 2016 · If you wanted to return a dictionary, you could use a dictionary comprehension instead of the list comprehension in Sven Marnach's answer like so: d = dict.fromkeys(range(100)) keys = random.sample(d.keys(), 10) sample_d = {k: d[k] for k in keys} Jul 27, 2022 · We can accomplish this task by one of the following options: Method 1: Use dict.items () Method 2: Use dict.keys () and dict.values () Method 3: Use next () Method 4: Use a for loop and dict.items () Method 5: Use Dictionary Comprehension. Sep 28, 2021 · That’s exactly what you’ll learn in the next few sections. Let’s get started! Use Python to Check if a Key Exists: Python keys Method. Python dictionary come with a built-in method that allows us to generate a list-like object that contains all the keys in a dictionary. Conveniently, this is named the .keys() method. Objects don't have names in Python, a name is an identifier that can be assigned to an object, and multiple names could be assigned to the same one. However, an object-oriented way to do what you want would be to subclass the built-in dict dictionary class and add a name property to it. Apr 21, 2016 · If you wanted to return a dictionary, you could use a dictionary comprehension instead of the list comprehension in Sven Marnach's answer like so: d = dict.fromkeys(range(100)) keys = random.sample(d.keys(), 10) sample_d = {k: d[k] for k in keys} Mar 24, 2023 · Get first key from a dictionary using keys () method. In python, dictionary is a kind of container that stores the items in key-value pairs. It also provides a function keys (), that returns an iterable sequence of all keys in the dictionary. We will use that to fetch all keys in the dictionary and then we will select first key from that ... Get Keys The keys () method will return a list of all the keys in the dictionary. Example Get a list of the keys: x = thisdict.keys () Try it Yourself » The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list. Example Apr 21, 2016 · If you wanted to return a dictionary, you could use a dictionary comprehension instead of the list comprehension in Sven Marnach's answer like so: d = dict.fromkeys(range(100)) keys = random.sample(d.keys(), 10) sample_d = {k: d[k] for k in keys} hesport
Jul 6, 2021 · For each dictionary in the list, you are trying to get the value for either key 'ipv4' or 'ipv6' and store it in ip_address. If there is no value for both, it will skip that element. Share Method 1: - To get the keys using .keys() method and then convert it to list. some_dict = {1: 'one', 2: 'two', 3: 'three'} list_of_keys = list(some_dict.keys()) print(list_of_keys) -->[1,2,3] Method 2: - To create an empty list and then append keys to the list via a loop. Nov 7, 2013 · One way to solve this, is by changing your last line: print(key, len([item for item in value if item])) ... Count occurrences of each key in python dictionary. 4. Jul 21, 2010 · for key in d: will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following: For Python 3.x: for key, value in d.items (): For Python 2.x: for key, value in d.iteritems (): To test for yourself, change the word key to poop. Apr 28, 2021 · The keys in a Python dictionary are already distinct from each other. You can't have the exact some keyword as a key twice in a Python dictionary. Therefore, counting the number of keys is the same as counting the number of distinct keys. May 27, 2023 · Creating a Dictionary. In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value. Apr 5, 2019 · For a very simple case like this, a comprehension, as in Ismail Badawi's answer is definitely the way to go.. But when things get more complicated, and you need to start writing multi-clause or nested comprehensions with complex expressions in them, it's worth looking into other alternatives. Sep 5, 2017 · Use dict.popitem () [0]. popitem () returns the only key-value pair in tuple: (key, value). If you do not want to mutate the original dictionary, make a copy first. Build a set and then pop: set (mydict).pop (). Simple performance comparisons in Python 3.9.6: Aug 3, 2022 · Adding to a Dictionary Using the = Assignment Operator. You can use the = assignment operator to add a new key to a dictionary: dict[key] = value. If a key already exists in the dictionary, then the assignment operator updates, or overwrites, the value. The following example demonstrates how to create a new dictionary and then use the ... Jul 25, 2018 · The dict contains only one key value. I am trying to get key from this dict, I tried d.keys()[0] but it returns IndexError, I tried this: list(d.keys())[0] It works just fine but I think it is not a good way of doing this because it creates a new list and then get it first index. Is there a better way to do this? I want to get the value too. 15. If you want to find the key by the value, you can use a dictionary comprehension to create a lookup dictionary and then use that to find the key from the value. lookup = {value: key for key, value in self.data} lookup [value] Share. Improve this answer. May 6, 2023 · Exercise 2: Merge two Python dictionaries into one. Exercise 3: Print the value of key ‘history’ from the below dict. Exercise 4: Initialize dictionary with default values. Exercise 5: Create a dictionary by extracting the keys from a given dictionary. Exercise 6: Delete a list of keys from a dictionary. Exercise 7: Check if a value exists ... To delete a key regardless of whether it is in the dictionary, use the two-argument form of dict.pop():. my_dict.pop('key', None) This will return my_dict[key] if key exists in the dictionary, and None otherwise. merchant build
Jun 21, 2009 · Add new keys to a nested dictionary. If a key has to be added to a dictionary that is nested inside a dictionary, dict.setdefault (or collections.defaultdict) is really useful. For example, let's try to add a new key-value pair to mydict only nested inside another key: 'address'. Apr 27, 2023 · Python | Initialize a dictionary with only keys from a list; Python | Get key with maximum value in Dictionary; Python | Split dictionary keys and values into separate lists; Python | Sum list of dictionaries with same key; Python | Filter the negative values from given dictionary; Python program to find the highest 3 values in a dictionary 665 5 17. Add a comment. 3. Here are separate functions to get a key, value or item: import random def pick_random_key_from_dict (d: dict): """Grab a random key from a dictionary.""" keys = list (d.keys ()) random_key = random.choice (keys) return random_key def pick_random_item_from_dict (d: dict): """Grab a random item from a dictionary ... Aug 3, 2022 · Adding to a Dictionary Using the = Assignment Operator. You can use the = assignment operator to add a new key to a dictionary: dict[key] = value. If a key already exists in the dictionary, then the assignment operator updates, or overwrites, the value. The following example demonstrates how to create a new dictionary and then use the ... Apr 21, 2016 · If you wanted to return a dictionary, you could use a dictionary comprehension instead of the list comprehension in Sven Marnach's answer like so: d = dict.fromkeys(range(100)) keys = random.sample(d.keys(), 10) sample_d = {k: d[k] for k in keys} Method 1: - To get the keys using .keys() method and then convert it to list. some_dict = {1: 'one', 2: 'two', 3: 'three'} list_of_keys = list(some_dict.keys()) print(list_of_keys) -->[1,2,3] Method 2: - To create an empty list and then append keys to the list via a loop. Method 1: - To get the keys using .keys() method and then convert it to list. some_dict = {1: 'one', 2: 'two', 3: 'three'} list_of_keys = list(some_dict.keys()) print(list_of_keys) -->[1,2,3] Method 2: - To create an empty list and then append keys to the list via a loop. Jul 15, 2015 · A call to without_keys(my_dict, ['keyB', 'keyC']) should return: { "keyA": 1 } I would like to do this in a one-line with a neat dictionary comprehension but I'm having trouble. My attempt is this: def without_keys(d, keys): return {k: d[f] if k not in keys for f in d} which is invalid syntax. How can I do this? May 2, 2023 · Otherwise, the function creates a dictionary with a single key-value pair as {keys[0]: value} and then calls itself recursively with the remaining keys keys[1:]. The result of the recursive call is merged with the current key-value pair using the unpacking operator ** to create a new dictionary. Jul 5, 2023 · We can call this method on our address_book as below: address_keys = address_book.keys () print (address_keys) This gives us: dict_keys ( ['Alicia Johnson', 'Jerry Smith', 'Michael Jonas']) The output returned above is a dict_keys object containing the keys of the address_book dictionary. Apr 5, 2019 · For a very simple case like this, a comprehension, as in Ismail Badawi's answer is definitely the way to go.. But when things get more complicated, and you need to start writing multi-clause or nested comprehensions with complex expressions in them, it's worth looking into other alternatives. Python doesn't magically turn your Element object into a list! The solution is simple. If you want your dictionary's values to be lists, then do that. When you add the first item, append a one-element list: d [groupIndex] = [list [i]] Alternatively, you can take a one-item slice of the original list. freehit.e uOct 20, 2012 · A solution to shorten the condition can be the following sample: dict = {} dict ['1'] = 10 dict ['1'] = dict.get ('1', 0) + 1 if '1' in dict else 1 print (dict) dict is the build-in name of dictionaries. The answer is functunally equivalent to the already accepted one with an unneccessary inline if function. Jul 25, 2018 · The dict contains only one key value. I am trying to get key from this dict, I tried d.keys()[0] but it returns IndexError, I tried this: list(d.keys())[0] It works just fine but I think it is not a good way of doing this because it creates a new list and then get it first index. Is there a better way to do this? I want to get the value too. Get Dictionary Value By Key With Get() in Python. To get the value of the key you want, you have to use the get() function using Python. The function requires a single argument which is the key in the dictionary. The below example contains 6 elements with both keys and the associated value of the dictionary. Use the method given below to get ... Jun 15, 2012 · I came across the dict method get which, given a key in the dictionary, returns the associated value. ... one might consider using dictionary.get ... Python 3.8 and ... get() Parameters. get() method takes maximum of two parameters: key - key to be searched in the dictionary; value (optional) - Value to be returned if the key is not found. The default value is None. To delete a key regardless of whether it is in the dictionary, use the two-argument form of dict.pop():. my_dict.pop('key', None) This will return my_dict[key] if key exists in the dictionary, and None otherwise. Objects don't have names in Python, a name is an identifier that can be assigned to an object, and multiple names could be assigned to the same one. However, an object-oriented way to do what you want would be to subclass the built-in dict dictionary class and add a name property to it. Aug 7, 2020 · So, I made this dictionary in python that has my class timetable. So, I created a dictionary for each lesson. friday = {} friday.setdefault('1st', []).append(8.40) # ... May 16, 2018 · 1. Another way to do this in one line while keeping the dictionary intact is: arbitrary_value = mydict.setdefault (*mydict.popitem ()) popitem () returns a tuple of (key, value) for the last item that was added into the dictionary and this pair is passed into setdefault as positional arguments. Aug 17, 2023 · Subtract Values from two Dictionaries using dictionary comprehension + keys () The combination of the above two can be used to perform this particular task. This is just a shorthand for the longer method of loops and can be used to perform this task in one line. Python3. test_dict1 = {'gfg' : 6, 'is' : 4, 'best' : 7} Therefore there is no first key. If you want to have a dict -like data structure with internal ordering of keys, have a look at OrderedDict. first_key = next (iter (data.keys ())) print (first_key) # 'Key1'. Dicts represent an unordered datatype, therefore there is no indexing. Oct 20, 2012 · A solution to shorten the condition can be the following sample: dict = {} dict ['1'] = 10 dict ['1'] = dict.get ('1', 0) + 1 if '1' in dict else 1 print (dict) dict is the build-in name of dictionaries. The answer is functunally equivalent to the already accepted one with an unneccessary inline if function. EVENT is a dictionary, that is correct. But the value corresponding to the key 'acknowledges' is a list, which you can see this from the brackets []. This list has only one entry which is a dictionary, denoted by the braces {}. So what you have is a dictionary in a list in a dictionary, which seemed strange to me. Sep 28, 2021 · That’s exactly what you’ll learn in the next few sections. Let’s get started! Use Python to Check if a Key Exists: Python keys Method. Python dictionary come with a built-in method that allows us to generate a list-like object that contains all the keys in a dictionary. Conveniently, this is named the .keys() method. legacy credit card login
Python has a set of built-in methods that you can use on dictionaries. Method. Description. clear () Removes all the elements from the dictionary. copy () Returns a copy of the dictionary. fromkeys () Returns a dictionary with the specified keys and value. Get Dictionary Value By Key With Get() in Python. To get the value of the key you want, you have to use the get() function using Python. The function requires a single argument which is the key in the dictionary. The below example contains 6 elements with both keys and the associated value of the dictionary. Use the method given below to get ... May 27, 2023 · Creating a Dictionary. In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value. May 27, 2023 · Creating a Dictionary. In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value. Aug 3, 2022 · Adding to a Dictionary Using the = Assignment Operator. You can use the = assignment operator to add a new key to a dictionary: dict[key] = value. If a key already exists in the dictionary, then the assignment operator updates, or overwrites, the value. The following example demonstrates how to create a new dictionary and then use the ... Nov 7, 2013 · One way to solve this, is by changing your last line: print(key, len([item for item in value if item])) ... Count occurrences of each key in python dictionary. 4. get() Parameters. get() method takes maximum of two parameters: key - key to be searched in the dictionary; value (optional) - Value to be returned if the key is not found. The default value is None. 2. Probably the quickest way to retrieve only the key name: mydic = {} mydic ['key_name'] = 'value_name' print mydic.items () [0] [0] Result: key_name. Converts the dictionary into a list then it lists the first element which is the whole dict then it lists the first value of that element which is: key_name. Share. Jul 6, 2021 · For each dictionary in the list, you are trying to get the value for either key 'ipv4' or 'ipv6' and store it in ip_address. If there is no value for both, it will skip that element. Share Objects don't have names in Python, a name is an identifier that can be assigned to an object, and multiple names could be assigned to the same one. However, an object-oriented way to do what you want would be to subclass the built-in dict dictionary class and add a name property to it. Jun 5, 2022 · you could get all the keys using recursion. def get_all_keys_rec(dic): keys = [key for key in dic] for val in dic.values(): if type(val)==dict: inner_keys = get_all_keys_rec(val) keys.extend(inner_keys) return keys print(get_all_keys_rec(data)) output: You can get any n key-value pairs though: n_items = take (n, d.items ()) This uses the implementation of take from the itertools recipes: from itertools import islice def take (n, iterable): """Return the first n items of the iterable as a list.""" return list (islice (iterable, n)) See it working online: ideone. pcu movie
2. Probably the quickest way to retrieve only the key name: mydic = {} mydic ['key_name'] = 'value_name' print mydic.items () [0] [0] Result: key_name. Converts the dictionary into a list then it lists the first element which is the whole dict then it lists the first value of that element which is: key_name. Share. Therefore there is no first key. If you want to have a dict -like data structure with internal ordering of keys, have a look at OrderedDict. first_key = next (iter (data.keys ())) print (first_key) # 'Key1'. Dicts represent an unordered datatype, therefore there is no indexing. EVENT is a dictionary, that is correct. But the value corresponding to the key 'acknowledges' is a list, which you can see this from the brackets []. This list has only one entry which is a dictionary, denoted by the braces {}. So what you have is a dictionary in a list in a dictionary, which seemed strange to me. If you want to make sure that the keys exist in your dictionary before you try to access them, you can use the set type. Convert the dictionary keys and your requested keys to sets. Then use the issubset() method. Objects don't have names in Python, a name is an identifier that can be assigned to an object, and multiple names could be assigned to the same one. However, an object-oriented way to do what you want would be to subclass the built-in dict dictionary class and add a name property to it.