简体   繁体   中英

Print input dict keys and values in one line

I want to print the all the keys in one line and all the values on the other.

a = eval(input())
for keys, values in a.items():
    klist = [keys, values]
    print([keys], "\n", [values])

The result is:

[123] 
 ['123']
[456] 
 ['456']
[789] 
 ['789']

But I want it to be like this:

[123, 456, 789]
['123', '456', '789']

Thanks in advance for the help.

Just use the keys() and values() methods of your dict:

a = {1: 10, 2: 20}
print(list(a.keys()), list(a.values()), sep='\n')

#[1, 2]
#[10, 20]

Note that the keys() and values() methods return iterators, so you must explicitely build a list from them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM