Python - Take a list and return random elements in ordered pairs (must be mutable) -


i have list want take random pieces out of 2 @ time. if list 1, 2, 3, 4, 5... want return 2 , 3 or 3 , 4 or 1 , 2 not 1 , 4 etc. know how use random , reading file , converted list called "word" can iterate on "word" , pull 2 random words right next each other , pass them in pair new variable?

you can pick random index 0 n-2 (where n length of list), pick index , it's adjacent value:

from random import randint lst = [1, 2, 3, 4, 5] index = randint(0, len(lst)-2) pair = lst[index:index+2]  # return [4, 5] or [1, 2], etc.. 

this returns list mutable.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -