Need to check all my variables for an ampersand, I put variables in a list and check using a for loop, but Python only changes value inside list

Advertisements I’ve got the following code, but unfortunately it only changes the value inside the list. Is there any way I can change the value outside the list, so it can be used later in the script? street_number = "100 & 102" street_name = "Fake Street" suburb = "Faketown" allvariables = [street_number, street_name, suburb] ampersand… Read More Need to check all my variables for an ampersand, I put variables in a list and check using a for loop, but Python only changes value inside list

Python DFS nested dictionary

Advertisements I’ve written a function which should be able to search a nested dictionary, using DFS, to find a specific value. The recursive element seems to be working fine, however, when the base case should return True, it simply doesn’t. obj = {‘a’: [{‘c’:’d’}, {‘e’:’f’}], ‘b’: [{‘g’:’h’}, {‘i’:’j’}]} def obj_dfs(obj, target): if type(obj) == dict:… Read More Python DFS nested dictionary

Migrating from Python 2.7 to 3.7 – difference between isinstance(obj, None) vs is None

Advertisements I have to migrate a project from Python 2.7 to 3.7. This line of code used to work in 2.7 if isinstance(obj, None): for some reason it doesn’t anymore. If I modify it this way: if isinstance(obj, type(None)): it will work though. But my question is, what is the difference between this call: isinstance(obj,… Read More Migrating from Python 2.7 to 3.7 – difference between isinstance(obj, None) vs is None

Adding a Key-Value pair in a list in an empty dictionary if the keys are the same

Advertisements How can I print a dictionary as a union of given dictionaries & appending the values if the keys are equal? Input: dict_1 = { "x":[1,"hello"], "y":[2,"world"], "z":3 } dict_2 ={ "p":4, "q":19, "z":123 } Output: dict_3={"x":[1,"hello"], "y":[2,"world"], "z":[3,123], "p":4, "q":19, } >Solution : Try this: Check out the inline comments for explanation of… Read More Adding a Key-Value pair in a list in an empty dictionary if the keys are the same

How can I return a list from a list of lists when searching a string and also force the comparison to lowercase?

Advertisements I have a list of lists as such: allTeams = [ [57, ‘Arsenal FC’, ‘Arsenal’, ‘ARS’], [58, ‘Aston Villa FC’, ‘Aston Villa’, ‘AVL’], [61, ‘Chelsea FC’, ‘Chelsea’, ‘CHE’], …] userIsLookingFor = "chelsea" for team in allTeams: if userIsLookingFor.lower() in any_element_of_team.lower(): print(team) > [61, ‘Chelsea FC’, ‘Chelsea’, ‘CHE’] I would basically look for the user’s… Read More How can I return a list from a list of lists when searching a string and also force the comparison to lowercase?

Add key to the nested dictionary recursively based on condition

Advertisements I have a multi level nested dictionary, trying to add a key required whenever there is type key in that dictionary. Code: def add_recursively(val): if ‘type’ in val and val[‘type’] != ‘object’: val[‘required’] = False return val elif ‘type’ in val and val[‘type’] == ‘object’ and ‘object_schema’ in val: return add_recursively(val[‘object_schema’]) else: val[‘required’] =… Read More Add key to the nested dictionary recursively based on condition