Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Python – Synchronized Split list with other

  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python data, we can have problem in which we need to perform split of a String, which has mapping with an element on other list, so need to perform mapping to different words formed due to split. This type of problem is peculiar, but can have application in many domains. Let’S discuss certain ways in which this task can be performed.

Input : test_list = [5, 6], str_list = [‘Gfg is best’, ‘I love Gfg’]
Output : [5, 5, 5, 6, 6, 6]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input : test_list = [5], str_list = [‘Gfg is best’]
Output : [5, 5, 5]



Method #1 : Using map() + zip() + enumerate() + split()
The combination of above functions can be used to solve this problem. In this, we perform the task of splitting values using split(), and corresponding element mapping using zip() and map(). The iteration is carried using enumerate().




# Python3 code to demonstrate working of 
# Synchronized Split list with other
# Using map() + zip() + enumerate() + split()
  
# initializing list
test_list = [5, 6, 3, 7, 4
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing String list 
str_list = ['Gfg', 'is best', 'I love', 'Gfg', 'and CS']
  
# Synchronized Split list with other
# Using map() + zip() + enumerate() + split()
temp = list(map(list, zip(*[(idx, sub) for idx, val in 
            enumerate(map(lambda x: x.split(), str_list), 1) for sub in val])))
res = []
for ele in temp[0]:
    res.append(test_list[ele - 1])
  
# printing result 
print("Mapped list elements : " + str(res))
Output :
The original list is : [5, 6, 3, 7, 4]
Mapped list elements : [5, 6, 6, 3, 3, 7, 4, 4]

 

Method #2 : Using chain.from_iterables() + zip()
This is yet another way in which this task can be performed. In this, we perform the task of flattening using chain.from_iterables() and zip() is used to perform parallel iteration.




# Python3 code to demonstrate working of 
# Synchronized Split list with other
# Using chain.from_iterables() + zip()
from itertools import chain
  
# initializing list
test_list = [5, 6, 3, 7, 4
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing String list 
str_list = ['Gfg', 'is best', 'I love', 'Gfg', 'and CS']
  
# Synchronized Split list with other
# Using chain.from_iterables() + zip()
res = list(chain.from_iterable([[sub1] * len(sub2.split()) for sub1, sub2 in zip(test_list, str_list)]))
  
# printing result 
print("Mapped list elements : " + str(res))
Output :
The original list is : [5, 6, 3, 7, 4]
Mapped list elements : [5, 6, 6, 3, 3, 7, 4, 4]



My Personal Notes arrow_drop_up
Recommended Articles
Page :