Documentos de Académico
Documentos de Profesional
Documentos de Cultura
TIPO DICT
1) {}
Forma de declarar una variable tipo Dict vacío.
EJM 1:
>>> variable1 = { }
>>>type(variable1)
<class ‘dict’>
>>>len(variable1)
>>>
2) len(A)
Nos devuelve el número de elementos que posee un diccionario
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>>len(variable1)
>>>
2) A.clear()
Elimina todos los elementos del diccionario “A”.
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>> variable1.clear()
>>>print (variable1)
{}
>>>
3) A.copy()
Permite crear un nuevo diccionario con los mismos elementos
del diccionario “A”.
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>>print (variable2)
{1:”UNO”,2:”DOS”,3:”TRES”}
>>>
NOTA
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>> variable1.keys()
dict_keys([1,2,3])
>>>
NOTA
5) A.values()
Permite crear un tipo de variable especial el cual devuelve SOLO
los valores (values) del diccionario “A”. Si se le antepone la función
list(), esta se convertirá a una lista.
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>> variable1.values()
dict.values([“UNO”,”DOS”,”TRES”])
>>>list(dict.values([“UNO”,”DOS”,”TRES”]))
[“UNO”,”DOS”,”TRES”]
>>>
5) A.update()
Permite añadir los elementos de un diccionario “B” a otro
diccionario “A”
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>>variable1.update(variable2)
>>>print (variable1)
{1:”UNO”,2:”DOS”,3:”TRES”,4:”CUATRO”,5:”CINCO”,6:”SEIS”}
5) A.items()
Devuelve una variable especial de pares de tuplas de clave-valor
(key-value) de un diccionario “A”. Puede ser convertido a lista o
tupla directamente con las funciones “list()” o “tuple()”
respectivamente.
EJM 1:
>>> variable1 = {1:”UNO”,2:”DOS”,3:”TRES”}
>>>variable2 = variable1.items()
>>>print (variable2)
dict_items((1,”UNO”),(2,”DOS”),(3,”TRES”))
>>>list(variable2)
[(1,”UNO”),(2,”DOS”),(3,”TRES”)]
>>>tuple(variable2)
((1,”UNO”),(2,”DOS”),(3,”TRES”))
>>>