We help IT Professionals succeed at work.
Private
Troubleshooting Question

collect list items in python

nav2567
nav2567 asked
on
26 Views
Last Modified: 2020-10-30
Hello,

I have this list:

mylist = {"my image length": 800, "my image width": 600, "my image resolution": 300} for key,

I am trying to have a loop to collect all keys (the strings inside the quotes), and all values (the numbers) in two separate list and prit.  

Please advise if the below python code is okay.  Thanks.

for key, value in mylist:
   print(key, ",")
   print(value, ",")


Comment
Watch Question

NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
No, it won't work - you need to use mylist.items().
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
mylist.keys() for the keys
mylist.values() for the values
both methods return a list

Author

Commented:
Sorry, my typo.

Let me ask again.  How do I output the below in one loop?

key: my image length, value: 500
key: my image width, value: 600
key: my image resolution, value: 300
All Keys: ['my image length', 'my image width', 'my image resolution']
All Values: [800, 600, 300]

My current code is this:

mylist = {"my image length": 800, "my image width": 600, "my image resolution": 300}
for key, value in mylist.items():
   print('key: ', key, ', value:', value)
   


Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
something like this maybe

for i in mylist.values():
    print(i, ',')
   
for k in mylist.keys():
    print(k, ',')
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
ignore the last one, it was sent at the same time you sent your update
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
for l in mylist.items():
    print('key ' +  l[0] + ' value ' + str(l[1]))
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
items() returns a list of tuples,
on a dictionary the tuple first item is the key, the second item is the value 
Analyst Assistant
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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