Files
30-seconds-of-code/snippets/sample.md
Angelos Chalaris 538108b040 Add sample snippet
2019-08-20 16:02:37 +03:00

343 B

title, tags
title tags
sample list,random,beginner

Returns a random element from an array.

Use randint() to generate a random number that corresponds to an index in the list, return the element at that index.

from random import randint

def sample(lst):
  return lst[randint(0, len(lst) - 1)]
sample([3, 7, 9, 11]) # 9