Python Forum
random from python dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random from python dictionary
#1
I have a dictionary and I need to select 3 items randomly (the key and value).

items = {

    'HP Laptop Charger':125,
    'Wireless Mouse': 75,
    'Wireless Keyboard':55,
    'GTA5':199,
    'Office 365':299,
    'Headphone':45,
    'AV':89
}

items_list3 = random.sample(items, k=3)
print(items_list3)
Thanks
Reply
#2
Line 12 add:
items_list3 = random.sample(items.items(), k=3)
Reply
#3
(Jun-17-2021, 09:59 PM)snippsat Wrote: Line 12 add:
items_list3 = random.sample(items.items(), k=3)

Thanks it is working. Now what I want is to assign 2 variables to hold the records. For example if 1st record selected is
'HP Laptop Charger':125, I need one variable to equal 'HP Laptop Charger' and other variable to equal 125.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subtract value from random dictionary key:value Tolein 1 2,430 Jun-12-2019, 02:02 PM
Last Post: perfringo
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 43,946 Apr-27-2019, 03:13 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020