Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Concatenate Random characters in String List

  • Last Updated : 19 Jul, 2021

Given a String list, perform concatenation of random characters.

Input : test_list = [“Gfg”, “is”, “Best”, “for”, “Geeks”] 
Output : “GiBfe” 
Explanation : Random elements selected, e.g G from Gfg, etc.
Input : test_list = [“Gfg”, “is”, “Best”] 
Output : “fst” 
Explanation : Random elements selected, e.g t from Best, etc. 
 

Method #1 : Using loop + random.choice()

In this, we extract random character using choice() and perform task of iteration using loop. The character concatenation is done using + operator.

Python3




# Python3 code to demonstrate working of
# Concatenate Random characters in String List
# Using loop + choice()
import random
 
# initializing list
test_list = ["Gfg", "is", "Best", "for", "Geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = ''
for ele in test_list:
     
    # Concatenating random elements
    res += random.choice(ele)
         
# printing results
print("Concatenated String : " + str(res))
Output
The original list is : ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String : Gsere

Method #2 : Using list comprehension + choice() + join()

In this, we perform task of getting random using choice() and concatenation is done using join().

Python3




# Python3 code to demonstrate working of
# Concatenate Random characters in String List
# Using list comprehension + choice() + join()
import random
 
# initializing list
test_list = ["Gfg", "is", "Best", "for", "Geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# characters joining using join()
res = ''.join([random.choice(ele) for ele in test_list])
         
# printing results
print("Concatenated String : " + str(res))
Output
The original list is : ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String : Gitrk


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!