Manish Sutradhar
Manish Sutradhar

Reputation: 39

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?

Upvotes: 0

Views: 67

Answers (3)

Omer
Omer

Reputation: 34

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)

Upvotes: 0

smrachi
smrachi

Reputation: 477

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

Upvotes: 0

Brian Rodriguez
Brian Rodriguez

Reputation: 4359

I think this is what you're looking for?

import operator

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

Upvotes: 2

Related Questions