Python | Scramble strings in list
Sometimes, while working with different applications, we can come across a problem in which we require to shuffle all the strings in the list input we get. This kind of problem can particularly occur in gaming domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension + sample() + join()
The combination of above functions can be used to solve this problem. In this, we need to disintegrate string into character list, scramble using sample(), rejoin them using join() and then remake list using list comprehension.
# Python3 code to demonstrate working of # Scramble strings in list # using list comprehension + sample() + join() from random import sample # initialize list test_list = ['gfg', 'is', 'best', 'for', 'geeks'] # printing original list print("The original list : " + str(test_list)) # Scramble strings in list # using list comprehension + sample() + join() res = [''.join(sample(ele, len(ele))) for ele in test_list] # printing result print("Scrambled strings in lists are : " + str(res)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Scrambled strings in lists are : ['fgg', 'is', 'btse', 'rof', 'sgeke']
Method #2 : Using list comprehension + shuffle() + join()
This is similar to the above method. The only difference is that shuffle() is used to perform scramble task than using sample().
# Python3 code to demonstrate working of # Scramble strings in list # using list comprehension + shuffle() + join() from random import shuffle # Utility function def perform_scramble(ele): ele = list(ele) shuffle(ele) return ''.join(ele) # initialize list test_list = ['gfg', 'is', 'best', 'for', 'geeks'] # printing original list print("The original list : " + str(test_list)) # Scramble strings in list # using list comprehension + shuffle() + join() res = [perform_scramble(ele) for ele in test_list] # printing result print("Scrambled strings in lists are : " + str(res)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Scrambled strings in lists are : ['fgg', 'is', 'btse', 'rof', 'sgeke']
Recommended Posts:
- Python | Scramble words from a text file
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python | Filter list of strings based on the substring list
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list of tuples to list of strings
- Python | Convert List of lists to list of Strings
- Python | Filter a list based on the given list of strings
- Python | Convert list of strings to list of tuples
- Python | How to sort a list of strings
- Python | Extract numbers from list of strings
- Python | Remove prefix strings from list
- Python | Sort list of dates given as strings
- Python | Finding strings with given substring in list
- Python - List Strings frequency in Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

