Reputation:
I have dict which got list, what I want is to display list elements in one line.
My dict
data = {'id':[1,2], 'name':['foo','bar'], 'type':['x','y']}
Expected output
name is foo and id is 1
name is bar and id is 2
My code
>>> data = {'id':[1,2], 'name':['foo','bar'], 'type':['x','y']}
>>> for key, value in data.items():
... print(value)
...
['foo', 'bar']
['x', 'y']
[1, 2]
>>>
Upvotes: -1
Views: 32
Reputation: 9572
You can use zip()
as:
for name, idx in zip(data['name'], data['id']):
print(f"name is {name} and id is {idx}")
Use format()
if you are using python version lower than 3.6:
for name, idx in zip(data['name'], data['id']):
print("name is {0} and id is {1}".format(name, idx))
Upvotes: 2