I'm writing a program and I need to scramble the letters of string
s from a list
in python. For instance I have a list
of string
s like:
l = ['foo', 'biology', 'sequence']
And I want something like this:
l = ['ofo', 'lbyoogil', 'qceeenus']
What is the best way to do it?
Thanks for your help!
Python has batteries included..
>>> from random import shuffle
>>> def shuffle_word(word):
... word = list(word)
... shuffle(word)
... return ''.join(word)
A list comprehension is an easy way to create a new list:
>>> L = ['foo', 'biology', 'sequence']
>>> [shuffle_word(word) for word in L]
['ofo', 'lbyooil', 'qceaenes']