How to not iterate in a dictionary? How to get the key? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to not iterate in a dictionary? How to get the key?

import json import requests from pprint import pprint def get_address(pendientes): #I want to improve function get_address and #get_addressv2. Specifically, I do not want to iterate in a #dictionary. Please help me. try: llaves = list(pendientes.keys()) print(llaves[3]+":",pendientes["age"]) except: print("Problemas en la función get_address") def get_addressv2(pendientes): #Proof that you cannot iterate in orden in a dicitionary try: key = list(pendientes.items())[1] print(key) except: print("Problemas en la función get_addressv2") def iteraccion(pendientes): for keys in pendientes.keys(): if keys =="age" : print(keys, ":", pendientes["age"]) def print_completeAddress(pendientes): for k,v in pendientes["address"].items(): print(k,":",v) def main(): try: input= "https://tools.learningcontainer.com/sample-json.json" response=requests.get(input) pendientes = json.loads(response.text) #pprint(pendientes) Si desea ver le Json descomente esta línea. get_address(pendientes) except: pprint("Problemas de cargue de JSON") if __name__== "__main__" : main() !!!Please help me to optimize this code and not to try to iterate in the dictionary"

12th Oct 2020, 5:14 PM
Rosana Rodríguez Milanés
Rosana Rodríguez Milanés - avatar
2 Answers
+ 2
Hi Rosana, it's hard to understand your question. From my guess, you want to access the key-value pairs, without iterating? In most cases, when you use a for-in loop, you could also use map() with a lambda function on iterables. Find more information here: https://www.python-kurs.eu/lambda.php
12th Oct 2020, 6:59 PM
Zen Coding
Zen Coding - avatar
0
Yes Zen, I want to access the key-value pairs, without iterating.
13th Oct 2020, 1:50 PM
Rosana Rodríguez Milanés
Rosana Rodríguez Milanés - avatar