

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print a Python dictionary randomly?
Python dictionary is not iterable. Hence it doesn’t have index to be randomized. Instead collection of its keys is iterable and can be randomized by shuffle() function in random module. Using shuffled keys we can print associated values.
>>> D1={"pen":25, "pencil":10, "book":100, "sharpner":5, "eraser":5} >>> import random >>> l=list(D1.keys()) >>> l ['pen', 'pencil', 'book', 'sharpner', 'eraser'] >>> random.shuffle(l) >>> l ['pencil', 'eraser', 'sharpner', 'book', 'pen'] >>> for k in l: print (k,D1[k])
pencil 10 eraser 5 sharpner 5 book 100 pen 25
- Related Questions & Answers
- How to print Python dictionary into JSON format?
- How to print all the keys of a dictionary in Python?
- How to print all the values of a dictionary in Python?
- How to print a value for a given key for Python dictionary?
- How to print the largest value from Python dictionary?
- How to Pretty print Python dictionary from command line?
- How to define a Python dictionary within dictionary?
- How to convert Javascript dictionary to Python dictionary?
- How to convert a String representation of a Dictionary to a dictionary in Python?
- How to update a Python dictionary values?
- How to sort a nested Python dictionary?
- How to create a dictionary in Python?
- How to sort a dictionary in Python?
- How to randomly select an item from a list in Python?
- How to randomly select an item from a tuple in Python?
Advertisements