Python | Convert String ranges to list

Sometimes, while working in applications we can have a problem in which we are given a naive string which provides ranges separated by a hyphen and other numbers separated by commas. This problem can occur across many places. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using sum() + split() + list comprehension + enumerate()
The combination of above functions can be used to perform these tasks. In this, the split is performed on hyphen and comma and accordingly range, numbers are extracted and compiled into a list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert String ranges to list
# Using sum() + list comprehension + enumerate() + split()
  
# initializing string 
test_str = "1, 4-6, 8-10, 11"
  
# printing original string 
print("The original string is : " + test_str)
  
# Convert String ranges to list
# Using sum() + list comprehension + enumerate() + split()
res = sum(((list(range(*[int(b) +
           for c, b in enumerate(a.split('-'))]))
           if '-' in a else [int(a)]) for a in test_str.split(', ')), [])
  
# printing result
print("List after conversion from string : " + str(res))

chevron_right


Output :



The original string is : 1, 4-6, 8-10, 11
List after conversion from string : [1, 4, 5, 6, 8, 9, 10, 11]

 

Method #2 : Using map() + split() + lambda
This task can also be performed using above functions. Similar to method above. The only difference is that we use map() and lambda function to reduce complex readability. Works only with Python2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python2 code to demonstrate working of
# Convert String ranges to list
# Using map() + lambda + split()
  
# initializing string 
test_str = "1, 4-6, 8-10, 11"
  
# printing original string 
print("The original string is : " + test_str)
  
# Convert String ranges to list
# Using map() + lambda + split()
temp = [(lambda sub: range(sub[0], sub[-1] + 1))(map(int, ele.split('-')))\
        for ele in test_str.split(', ')]
  
res = [b for a in temp for b in a]
  
# printing result
print("List after conversion from string : " + str(res))

chevron_right


Output :

The original string is : 1, 4-6, 8-10, 11
List after conversion from string : [1, 4, 5, 6, 8, 9, 10, 11]



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.