How to create a new dictionary where all keys and values are converted to string type? If string length is more than 5 symbols it must be excluded from the answer (both the key and value)
My code now looks like this:
file1data = {"1":"2",2:5,"12dade":-1,"1231":14,"-2":7}
key_values = file1data.items()
new_dictionary = {str(key):str(value) for key, value in key_values}
print((str(new_dictionary)))
It can covert all values and keys to a string type, but not detect if length is more than 5.
For example if is given this dictionary: {"1":"2",2:5,"12dade":-1,"1231":14,"-2":7}
The result should be: {"1":"2","2":"5","1231":"14","-2":"7"}
>Solution :
Add if statement inside dict comprehension like so
file1data = {"1": "2", 2: 5, "12dade": -1, "1231": 14, "-2": 7}
key_values = file1data.items()
new_dictionary = {str(key): str(value) for key, value in key_values if len(str(key)) <= 5 and len(str(value)) <= 5}
print((str(new_dictionary)))