Python – Convert delimiter seperated Mixed String to valid List

Given a string with elements and delimiters, split elements on delimiter to extract with elements ( including containers).

Input : test_str = “6*2*9*[3, 5, 6]*(7, 8)*8*4*10”, delim = “*”
Output : [6, 2, 9, [3, 5, 6], (7, 8), 8, 4, 10]
Explanation : Containers and elements separated using *.

Input : test_str = “[3, 5, 6]*(7, 8)*8*4*10”, delim = “*”
Output : [[3, 5, 6], (7, 8), 8, 4, 10]
Explanation : Containers and elements separated using *.

Method #1 :  Using loop + eval() + split()

This is one way in which this task can be done. In this the separation is done using split() and eval() does the important task of performing evaluation of data types to be containers or simpler elements. 



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert delimiter seperated Mixed String to valid List
# Using loop + split() + eval()
  
# initializing string
test_str = "6# 2# 9#[3, 5, 6]#(7, 8)# 8# 4# 10"
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing delim 
delim = "#"
  
# spliting using split()
temp = test_str.split(delim) 
res = []
  
# using loop + eval() to convert to 
# required result
for ele in temp:
  res.append(eval(ele))
  
# printing result 
print("List after conversion : " + str(res)) 

chevron_right


Output

The original string is : 6#2#9#[3, 5, 6]#(7, 8)#8#4#10
List after conversion : [6, 2, 9, [3, 5, 6], (7, 8), 8, 4, 10]

Method #2 : Using eval() + split() + list comprehension

This is yet another way in which this task can be performed. In this, we perform similar task as above method. The only difference being that entire logic is encapsulated as one liner using list comprehension.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert delimiter seperated Mixed String to valid List
# Using eval() + split() + list comprehension
  
# initializing string
test_str = "6# 2# 9#[3, 5, 6]#(7, 8)# 8# 4# 10"
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing delim 
delim = "#"
  
# encapsulating entire result in list comprehension 
res = [eval(ele) for ele in test_str.split(delim)]
  
# printing result 
print("List after conversion : " + str(res)) 

chevron_right


Output

The original string is : 6#2#9#[3, 5, 6]#(7, 8)#8#4#10
List after conversion : [6, 2, 9, [3, 5, 6], (7, 8), 8, 4, 10]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.