Python | How to append elements to a list randomly
Posted
by MMRUser
on Stack Overflow
See other posts from Stack Overflow
or by MMRUser
Published on 2010-03-19T07:03:46Z
Indexed on
2010/03/19
7:11 UTC
Read the original article
Hit count: 126
python
Is there a way to append elements to a list randomly, built in function
ex:
def random_append():
lst = ['a']
lst.append('b')
lst.append('c')
lst.append('d')
lst.append('e')
return print lst
this will out put ['a', 'b', 'c', 'd', 'e']
But I want it to add elements randomly and out put something like this:
['b', 'd', 'b', 'e', 'c']
And yes there's a function random.shuffle() but it shuffles a list at once which I don't require, I just want to perform random inserts only.
© Stack Overflow or respective owner