[Python] Printing elements of dictionary line by line

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dictionary in a file and i should write a python code to print the keys and values in seperate lines.(without using .keys() and .values().

    For eg: dict={"the":"1", "and":"2"} should return as

    the:1
    and:2


    Here is my code i tried. I am new to python dictionary. Please help me fix this.

    dict2 = {}

    f= open("dict.txt", 'r')
    for line in f:
    it = line.split()
    k, v = it[0], it[1:]
    dict2[k] = v
    return (dict2)

    Continue reading...

Compartilhe esta Página