-3

So I have a list (shown below) and I need to randomly access one of the dictionaries in a list, and print it out:

e.g. Instagram, 346, Social media platform, United States

I've tried to google and search for it, but whatever I tried it didn't work. I know how to print out the whole list, but I don't know how to print a single dictionary randomly.

    data = [
{
    'name': 'Instagram',
    'follower_count': 346,
    'description': 'Social media platform',
    'country': 'United States'
},
{
    'name': 'Cristiano Ronaldo',
    'follower_count': 215,
    'description': 'Footballer',
    'country': 'Portugal'
},
{
    'name': 'Ariana Grande',
    'follower_count': 183,
    'description': 'Musician and actress',
    'country': 'United States'
}
]
FraneCal
  • 69
  • 8
  • *whatever I tried it didn't work.* Please share what you tried, then we can help you figure out what is wrong. – Cow Nov 17 '22 at 14:25
  • Does this answer your question? [Generate random integers between 0 and 9](https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9) or better yet [How can I randomly select an item from a list?](https://stackoverflow.com/questions/306400/how-can-i-randomly-select-an-item-from-a-list) – Abdul Aziz Barkat Nov 17 '22 at 14:26
  • 2
    Look into `random.choice`. It doesn't matter than the objects in the list are dictionaries. Choosing a random object from a list is a standard problem with a standard solution. – John Coleman Nov 17 '22 at 14:27
  • When I use random.choice it prints out the whole list, and I just want to print out a single dictionary. – FraneCal Nov 17 '22 at 14:30
  • @FraneCal `random.choice(data)` does give me a single dictionary... – Abdul Aziz Barkat Nov 17 '22 at 14:32
  • @AbdulAzizBarkat Okey, it does to me to now, but it didn't before. I don't know why. – FraneCal Nov 17 '22 at 14:34

2 Answers2

1

You can use random.choice:

import random

random.choice(data)
Runinho
  • 439
  • 1
  • 6
1
import random
print(random.choice(data))

#Output1
{'name': 'Cristiano Ronaldo', 'follower_count': 215, 'description': 'Footballer', 'country': 'Portugal'}

print(random.choice(data))
#Output2
{'name': 'Ariana Grande', 'follower_count': 183, 'description': 'Musician and actress', 'country': 'United States'}

# UPDATE 2 (To select more than one key from same random dictionary)

random_index = random.randrange(len(data)-1)
print(data[random_index]["name"])
print(data[random_index]["follower_count"])
Bharat Adhikari
  • 334
  • 1
  • 6
  • I want to print only one random dictionary from a list. – FraneCal Nov 17 '22 at 14:32
  • Check the above result. we will get only one random dictionary. – Bharat Adhikari Nov 17 '22 at 14:34
  • Okay, and how can I only print out, let's say the name without the rest in the dictionary, e.g. Christiano Ronaldo? – FraneCal Nov 17 '22 at 14:36
  • 2
    print( random.choice(data)["name"] ) – Bharat Adhikari Nov 17 '22 at 14:38
  • One more thing and I'm done, can I print out more values from a single dictionary, e.g. (this doesn't work but just to show you): print(random.choice(data)["name"]["follower_count"]), so to print out: Christiano Ronaldo, 215 – FraneCal Nov 17 '22 at 14:41
  • In Python dictionary, "keys" like "name","follower_count" are unique so . Either you do `print(random.choice(data)["name"])` and `print(random.choice(data)["follower_count"])` in separate line Or, run a for loop. – Bharat Adhikari Nov 17 '22 at 14:49
  • If I print it out in a separate line I get values from a different dictionary and I want to get values from the same dictionary. How can I do that? – FraneCal Nov 17 '22 at 14:56
  • 1
    You are correct. I have made an #UPDATE on above solution . It will work – Bharat Adhikari Nov 17 '22 at 15:04
  • @FraneCal you are in need of a good Python tutorial. `random.choice(data)` gives you a random dictionary from your list, can you not simply assign this to a variable and use it? `random_dict = random.choice(data)` and then `print(random_dict["name"])` etc.? – Abdul Aziz Barkat Nov 17 '22 at 15:12
  • @AbdulAzizBarkat I am learning Python at the moment. – FraneCal Nov 17 '22 at 15:18