Change language

Python | Scramble strings in a list

|

Method # 1: Using List Comprehension + sample () + join ()
A combination of the above functions can be used to solve this problem. In this case, we need to split the string into a list of characters, scramble them with sample (), concatenate them with join (), and then rewrite the list using a list comprehension.

# Python3 code to demonstrate how it works
# Scramble lines in a list
# using a list comprehension + sample () + join ()

from random import sample 

  
# initialize the list

test_list = [ ’gfg’ , ’ is’ , ’best’ , ’ for’ , ’geeks’ ]

  
# print original list

print ( "The original list:" + str (test_list))

 
# Scramble lines in the list
# using a list comprehension + sample () + join ()

res = [’’ .join (sample (ele, len (ele))) for ele i n test_list]

 
# print result

print ( " Scrambled strings in lists are: " + str (res))

Output:

 The original list: [’gfg’,’ is’, ’best’,’ for’, ’geeks’] Scrambled strings in lists are: [’ fgg’, ’is’,’ btse’, ’rof’,’ sgeke’] 

Method # 2: Using comprehension list + shuffle () + join ()
This is similar to the above method. The only difference is that shuffle () is used to perform the scrambling task, rather than sample () is used.

# Python3 code to demonstrate how it works
# Scramble lines in the list
# use comprehension list + shuffle () + join ()

from random import shuffle

 
# Helper function

def perform_scramble (ele):

  ele = list (ele)

  shuffle (ele)

return ’’ .join (ele)

 
# initialize the list

test_list = [ ’gfg’ , ’is’ , ’ best’ , ’for’ , ’ geeks’ ]

 
# print original list

print ( "The original list:" + str (test_list))

  
# Scramble lines in the list
# use comprehension list + shuffle () + join ()

res = [perform_scramble (ele) for ele in test_list]

 
# print result

print ( "Scrambled strings in lists are:" + str (res))

Output:

 The original list: [’gfg’,’ is’, ’best’,’ for’, ’geeks’] Scrambled strings in lists are: [’fgg’,’ is’, ’btse’,’ rof’, ’sgeke’] 

Python | Scramble strings in a list Python functions: Questions

Python | Scramble strings in a list String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method