简体   繁体   中英

How to print values from dictionary of your choice in a single line ? IN PYTHON

Suppose this is a dictionary : {'name': 'Instagram', 'follower_count': 346, 'description': 'Social media platform', 'country': 'United States'}

and i want my output like : Instagram, Social media platform, United States

How do I achieve this?

I think this is what you're looking for?

import operator

items_getter = operator.itemgetter('name', 'description', 'country')
print(', '.join(items_getter(dictionary)))

Use the key in condition to whatever value you want to eliminate

for i in thisdict:
    if i == "follower_count":
        continue
    else:
        print(thisdict[i])

Or you may also use .items method to get key,values and then continue

for k,v in thisdict.items():
    if k=="follower_count":
        continue
    else:
        print(v)

This is the simplest way you can get what you want:

dct = {
    'name': 'Instagram', 
    'follower_count': 346, 
    'description': 'Social media platform', 
    'country': 'United States'
}

print(f'{dct['name']}, {dct['description']}, {dct['country']}')

Output:

Instagram, Social media platform, United States

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM