We help IT Professionals succeed at work.
Private
Troubleshooting Question

python script assistance

nav2567
nav2567 asked
on
28 Views
Last Modified: 2020-10-30
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.
Comment
Watch Question

Author

Commented:
basically, collect the keys in a separate list and print.  Collect the values in a separate list and print. 
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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?


Analyst Assistant
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
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.  

NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
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?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions