Coding with AI

Code Faster. Think Smarter. Ship Better—with AI.

Stop fighting boilerplate and busywork. Coding with AI shows professional Python developers how to use AI tools to accelerate design, coding, testing, debugging, and documentation—without sacrificing quality or control. Learn proven prompts, real workflows, and practical techniques you’ll use on the job every day.

Explore the book ->


This Tutorial Explores the Basics of a Useful Programming Function

If you’re dealing with lists in Python, it’s pretty likely that you will want to pick a random item. Whether for creating unique experiences, or for creating fun and engaging content, having a way to …

Updated November 12, 2023

If you’re dealing with lists in Python, it’s pretty likely that you will want to pick a random item. Whether for creating unique experiences, or for creating fun and engaging content, having a way to randomly select an item is indispensable. Here, we will learn how to do this efficiently.

In Python, there are several ways to pick a random element from a list. Here’s one of the simplest ways, by using the random module:

import random
list = ["apple", "banana", "cherry"]
item = random.choice(list)
print(item)

The random.choice() function takes a list as an argument and returns a randomly chosen element from it. In this example, the print statement will output either “apple”, “banana” or “cherry”.

Another way to pick a random item is by directly accessing the list at a random index:

list = ["apple", "banana", "cherry"]
index = random.randint(0, len(list)-1)
item = list[index]
print(item)

This will give you the same result as the previous example because Python’s lists are zero-indexed and randint function generates an integer within a specified range (inclusive).

Random selection from a list is a great way to create unique experiences, make content more engaging and entertaining for your users. The choice of randomness can often be influenced by the context, such as creating games where specific items are chosen with some frequency based on their rarity or having a certain item chosen more frequently than others.

Coding with AI

AI Is Changing Software Development. This Is How Pros Use It.

Written for working developers, Coding with AI goes beyond hype to show how AI fits into real production workflows. Learn how to integrate AI into Python projects, avoid hallucinations, refactor safely, generate tests and docs, and reclaim hours of development time—using techniques tested in real-world projects.

Explore the book ->