'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?
Solution 1:[1]
I think this is what you're looking for?
import operator
items_getter = operator.itemgetter('name', 'description', 'country')
print(', '.join(items_getter(dictionary)))
Solution 2:[2]
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
Solution 3:[3]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brian Rodriguez |
| Solution 2 | smrachi |
| Solution 3 |
