loading...

Print a Python Dictionary Randomly

highcenburg profile image Vicente G. Reyes Originally published at highcenburg.tech.blog on ・2 min read

On the last quarter of 2019, I saw a trend on twitter wherein developers was planning what challenge they’d do to develop their skill or learn a new technology. I was overwhelmed by their drive – some did 30 projects in 30 days, another did 100 projects in 100 days. I, on the other side of the world, decided that I won’t give myself a hard time and would do 12 projects in 12 months in 2020.

The ideas are:

  • A Restaurant Management App
  • A Customer Relationship Management App
  • An eCommerce Website using Rails
  • A Freelance Job board
  • An Uptime Robot clone
  • A Social Network
  • An AI-powered Music Mastering App
  • A Time Tracker App
  • An Online Image Compressor
  • A Biometrics App
  • An Earthquake Detector app &
  • A Google Drive/Dropbox clone

It wasn’t easy to come up with ideas, and until an hour ago, I didn’t realize which one I’d do first.

After a minute or two of search in a search engine, I realized I could use the random library of Python to shuffle the list so I can get started on my first project.

""" I use iTerm as my main CLI """
~ python3 # initialize the python shell
Python 3.7.6 (default, Dec 30 2019, 19:38:26)
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

# create the dictionary
>>> app = {'resto': 1, 'crm': 2, 'ecom_rails': 3, 'jobboard': 4, 
'uptime_robot': 5, 'socialNetwork': 6, 'musicMastering': 7, 'timeTracker': 8, 
'onlineImageCompressor': 9, 'biometrics': 10, 'earthquakeDetector': 11, 
'dropboxClone': 12}

# import the random library
>>> import random

# create a variable for the dictionary
>>> d = list(app.keys())

# check the variable
>>> d
['resto', 'crm', 'ecom_rails', 'jobboard', 'uptime_robot', 'socialNetwork', 
'musicMastering', 'timeTracker', 'onlineImageCompressor', 'biometrics', 
'earthquakeDetector', 'dropboxClone']

# shuffle the dictionary
>>> random.shuffle(d)
>>> d
['dropboxClone', 'onlineImageCompressor', 'jobboard', 'ecom_rails', 'uptime_robot', 
'socialNetwork', 'earthquakeDetector', 'crm', 'biometrics', 'timeTracker', 'resto', 
'musicMastering']

I’m happy that the AI-powered project is last on the list. Why? I haven’t touched AI yet – I can use the 11 months to learn AI. Wish me luck!

Here’s the list of projects for a better user-experience.

  • Dropbox/Google Drive Clone
  • Online Image Compressor
  • Freelance Job board
  • eCommerce in rails
  • Uptime Robot clone
  • Social Network App
  • Eathquake Detector
  • CRM
  • Biometrics
  • Time Tracker
  • Restaurant Management App
  • AI-powered Music Mastering App

2020 started great – I already launched 2 podcast episodes – subscribe on our YouTube channel here!

Buy Me A Coffee

Posted on Jan 9 by:

highcenburg profile

Vicente G. Reyes

@highcenburg

A Self-Directed Learner, a Freelance Web Developer, a Volunteer Developer at Project Website, & DEV Tag-Moderator, one of the brains of The Underwearkers on Facebook & a podcast host.

Discussion

markdown guide
 

I do get the point of your post but dictionaries in python are stored as random anyways. Maybe random is a strong word here but at least we can say they are not guaranteed to be in order. So if you define a dict and just print it, you will see that it is not outputting it in order that you defined.

Edit: According to jpittiglio's reply this is not the case anymore for python 3.7. Please see the SO link below.

 

No longer true in modern Python - ref: stackoverflow.com/questions/399803...

tl;dr - Dictionaries are insertion ordered as of 3.6 as an implementation detail, and a language feature as of 3.7

 

Did not know that at all. Thanks for the update! :)

 

Well what's the point of using a dictionary, if you're just gonna create a list
of the keys anyways?

 

The randomly chosen key could then be used on the original data structure (dict) to pull back additional details that may have been stored, such as a URL.