Select a random Item from a Python List - Python Recipes

preview_player
Показать описание

SUBSCRIBE TO OUR YOUTUBE CHANNEL

#python #recipes #dataanalytics #datascience #pandas #dataframe #random

Imagine that you want to build a magic ball functionality in a travel app where cities are picked at random from a pre-defined list of cities. In this video we review how to do that using the python random library and the choices() methods.

The process is quite simple. you just need to import the random library and then provide the list as argument to return one random items from the list. See code below:

import random
cities_to_visit=['Miami','New York','Paris','Bogota', 'Madrid']

The implementation above assumes that every time you run the code all cities are presented as possible choices, which formally is the case of random selection with replacement.

import random
cities_to_visit=['Miami','New York','Paris','Bogota', 'Madrid']
while len(cities_to_visit)!=0:
Рекомендации по теме
Комментарии
Автор

why in the heck is this channel so underrated it really helped me in a sticky situation

christiansquare
Автор

Thanks so much I have a project to do and this helped out alot! New subscriber!

jakenirmal
Автор

This is great. Could only be better if the intro wasn’t 30 seconds long. Other than that short and to the point.

KenrickLeiba
Автор

Nice video. How can I limit random to choose alle the items from list only once?

vecchiemaniere