20 lines
262 B
Markdown
20 lines
262 B
Markdown
---
|
|
title: sample
|
|
tags: list,random,beginner
|
|
---
|
|
|
|
Returns a random element from a list.
|
|
|
|
- Use `random.choice()` to get a random element from `lst`.
|
|
|
|
```py
|
|
from random import choice
|
|
|
|
def sample(lst):
|
|
return choice(lst)
|
|
```
|
|
|
|
```py
|
|
sample([3, 7, 9, 11]) # 9
|
|
```
|