Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

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.
Avatar of nav2567
nav2567
Flag of United States of America image

ASKER

basically, collect the keys in a separate list and print.  Collect the values in a separate list and print. 
Avatar of Norie
Norie

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}')

Open in new window

Avatar of nav2567

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?


ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

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.  

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?