nav2567
asked on
python script assistance
Hello,
I am learning how to write a python script to read a dataset and print.
For example, here is my sample set:
mylist = {"my image length": 800, "my image width": 600, "my image resolution": 300}
I need the output looks like this:
key: my image length, value: 800
key: my image width, value: 600
key: my image resolution, value: 300
I am thinking about the below not not sure how to do it.
for info in mylist:
print("key: ",info,",","value: ")
Please advise.
I am learning how to write a python script to read a dataset and print.
For example, here is my sample set:
mylist = {"my image length": 800, "my image width": 600, "my image resolution": 300}
I need the output looks like this:
key: my image length, value: 800
key: my image width, value: 600
key: my image resolution, value: 300
I am thinking about the below not not sure how to do it.
for info in mylist:
print("key: ",info,",","value: ")
Please advise.
Try this.
mylist = {"my image length": 800, "my image width": 600, "my image resolution": 300}
for key, value in mylist.items():
print(f'key: {key}, value: {value}')
ASKER
I got it now. Thanks.
If I want to print two more lines in the end:
All Key: ['my image length', 'my image width', 'my image resolution]
All Values: [800, 600, 300]
how do I do that?
If I want to print two more lines in the end:
All Key: ['my image length', 'my image width', 'my image resolution]
All Values: [800, 600, 300]
how do I do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Norie,
Can you explain what f'key does? I use the below in line 4 without line 6 & 7 and it works
print('key:', key, ', value:', value)
I do not have a success in line 6 & 7 yet.
Can you explain what f'key does? I use the below in line 4 without line 6 & 7 and it works
print('key:', key, ', value:', value)
I do not have a success in line 6 & 7 yet.
Preceding a string with f is a way of formatting strings, have a look here.
I'm using it to interpolate the variables into the string.
There are various other ways you can do that, as you've shown.
What problems are you having with the last 2 lines?
I'm using it to interpolate the variables into the string.
There are various other ways you can do that, as you've shown.
What problems are you having with the last 2 lines?
ASKER