{"id":724,"date":"2024-03-30T10:49:25","date_gmt":"2024-03-30T10:49:25","guid":{"rendered":"https:\/\/javigomez.org\/?p=724"},"modified":"2025-09-29T20:33:15","modified_gmt":"2025-09-29T20:33:15","slug":"04-05-python-diccionarios","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/03\/30\/04-05-python-diccionarios\/","title":{"rendered":"04.05 Python \u2013 Diccionarios"},"content":{"rendered":"\n<p>Un diccionario en Python es una <strong>colecci\u00f3n de pares clave-valor<\/strong>. Las <strong>claves son \u00fanicas y los valores pueden ser de cualquier tipo<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mi_diccionario = {clave1: valor1, clave2: valor2, clave3: valor3, ...}\n\n<strong>Agregar elementos: <\/strong>\n\ndiccionario = {\"Mar\u00eda\": 80, \"Pedro\": 70}\n\ndiccionario&#91;\"Juan\"] = 90\n\n<strong>Obtener elementos: <\/strong>\n\nvalor = diccionario&#91;\"Juan\"]\n\nprint(valor)\n\n# 90\n\n<strong>Eliminar elementos:<\/strong> \n\ndiccionario.pop(\"Juan\")\n\n<strong>Iterar sobre los elementos: <\/strong>\n\nfor key, value in diccionario.items():\n    print(key, value)\n\n#Mar\u00eda 80\n#Pedro 70\n\n<strong>Verificar la existencia de una clave: <\/strong>\n\nif \"Juan\" in diccionario:\n    print(\"La clave existe\")\nelse:\n    print(\"La clave no existe\")\n\n# La clave existe\n\n<strong>Obtener la longitud de un diccionario: <\/strong>\n\nlongitud = len(diccionario)\n\nprint(longitud)\n\n# 2\n<strong>Ordenar diccionarios<\/strong>\n\nOrdenar por claves:\n\n<strong>a) Funci\u00f3n sorted():<\/strong>\n\nLa funci\u00f3n sorted() crea una nueva lista ordenada con las claves del diccionario.\n\ndiccionario = {\"a\": 1, \"c\": 3, \"b\": 2, \"d\": 4}\n\nclaves_ordenadas = sorted(diccionario.keys())\n\nprint(diccionario)  # Imprime: {'a': 1, 'c': 3, 'b': 2, 'd': 4}\nprint(claves_ordenadas)  # Imprime: &#91;'a', 'b', 'c', 'd\u2019]\n\n<strong>b) Bucle for:<\/strong>\n\n\ndiccionario = {\"a\": 1, \"c\": 3, \"b\": 2, \"d\": 4}\n\nfor clave in sorted(diccionario.keys()):\n  print(clave, diccionario&#91;clave])  # Imprime: a 1, b 2, c 3, d 4\n\n<strong>2. Ordenar por valores:<\/strong>\n\na) Funci\u00f3n sorted() con lambda:\n\n\ndiccionario = {\"a\": 1, \"c\": 3, \"b\": 2, \"d\": 4}\n\nvalores_ordenados = sorted(diccionario.values())\n\nprint(diccionario)  # Imprime: {'a': 1, 'c': 3, 'b': 2, 'd': 4}\nprint(valores_ordenados)  # Imprime: &#91;1, 2, 3, 4]\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"PYTHON U04 05 Diccionario\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/Qkj18HJEQ48?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Un diccionario en Python es una colecci\u00f3n de pares clave-valor. Las claves son \u00fanicas y los valores pueden ser de cualquier tipo.<\/p>\n","protected":false},"author":1,"featured_media":725,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[118,277],"class_list":["post-724","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-diccionario","tag-python"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":864,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/724\/revisions\/864"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/725"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}