E
E
EvanGun2021-08-03 14:28:27
Python
EvanGun, 2021-08-03 14:28:27

Outputting a dictionary of lists without brackets, how to do it?

I have a final dictionary of lists:
{'Vika': [2, 0, 0, 2, 0], 'Danya': [2, 1, 0, 1, 3], 'Lisa': [2, 2, 0 , 0, 6]}

I need it to be output in the following format:
Vika:2 0 0 2 0
Danya:2 1 0 1 3
Lisa:2 2 0 0 6

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
soremix, 2021-08-03
@SoreMix

d = {'Вика': [2, 0, 0, 2, 0], 'Даня': [2, 1, 0, 1, 3], 'Лиза': [2, 2, 0, 0, 6]}

for key, value in d.items():
    print(key, ':', ' '.join(str(x) for x in value))

M
Michael, 2021-08-03
@lob4Noff

my_dict = {
    'Вика': [2, 0, 0, 2, 0], 
    'Даня': [2, 1, 0, 1, 3], 
    'Лиза': [2, 2, 0, 0, 6]
    }

for key in my_dict:
    print(f"{key}: {str(my_dict[key]).replace('[', '').replace(']', '')}")

A
Ara2, 2021-08-03
@Ara2

d = {'Вика': [2, 0, 0, 2, 0], 'Даня': [2, 1, 0, 1, 3], 'Лиза': [2, 2, 0, 0, 6]}
[print(i,':',*d[i]) for i in d]

A
Alexander, 2021-08-03
@Survtur

I hope this makes it clearer how things work. Is each line clear?

d = {'Вика': [2, 0, 0, 2, 0],
     'Даня': [2, 1, 0, 1, 3],
     'Лиза': [2, 2, 0, 0, 6]}

for key, values in d.items():
    string_values = [str(i) for i in values]
    joined = ", ".join(string_values)
    print(f"{key}: {joined}")

L
link_vrb, 2021-08-11
@link_vrb

d={'Vika': [2, 0, 0, 2, 0], 'Danya': [2, 1, 0, 1, 3], 'Lisa': [2, 2, 0, 0, 6] }
for i in d.keys():
print(f'{i}: {d[i]}')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question