Skip to content

Convert String to List of Tuples in Python Malli Spark By {Examples}

  • by

How to convert a string to a list of tuples in python? To convert a string to a list of tuples, you should split the string into individual elements and then group those elements into tuples. You need to specify the format of the string and the pattern to extract the values for each tuple.

You can convert a string to a list of tuples by using many ways, for example, by using zip()+split()+ list slicing, list comprehension, re.findall(), and iter()+split()+next()+generator expression. In this article, I will explain convert a string to a list of tuples by using all these methods with examples.

1. Quick Examples of Convert String to List of Tuples

If you are in a hurry, below are some quick examples of how to convert a string to a list of tuples in python.

# Quick examples of converting string to list of tuples

# Example 1: Convert string to a list of tuples
# Using zip() + split() + list slicing
string = “Spark By Examples .com is the Best Tutorial”
temp = string.split()
result = list(zip(temp[:-1:2], temp[1::2]))

# Example 2: Convert String to list of tuples
# Using list comprehension + zip()
temp = string.split()
result = [(temp[i], temp[i+1]) for i in range(0, len(temp), 2)]

# Example 3: Using list comprehension + zip()
result = [(string.split()[i], string.split()[i+1]) for i in range(0, len(string.split())-1, 2)]

# Example 4: Using re.findall() and list comprehension
matches = re.findall(r’b(w+)b b(w+)b’, string)
result = [(match[0], match[1]) for match in matches]

# Example 5: Using iter() + split() + next() + generator expression
temp = iter(string.split())
result = [(element, next(temp)) for element in temp]

2. Convert String to List of Tuples in Python

To convert a string to a list of tuples, first, you need to split the string by a delimiter and slice the list into two lists by selecting every other element and zip the two lists to create tuples and finally convert them to list.

# Initialize string
string = “Spark By Examples .com is the Best Tutorial”
print(“Orginal String: “,string)

# Convert string to list of tuples
# Using zip() + split() + list slicing
temp = string.split()
result = list(zip(temp[:-1:2], temp[1::2]))
print(“Result: “,result)

# Output
# Original String: Spark By Examples .com is the Best Tutorial
# Result: [(‘Spark’, ‘By’), (‘Examples’, ‘.com’), (‘is’, ‘the’), (‘Best’, ‘Tutorial’)]

Here, the split() method splits the string into a list of words. The temp[:-1:2] slice takes every second word starting from the first word, while the temp[1::2] slice takes every second word starting from the second word. The zip() function then combines the two slices into a list of tuples, where each tuple contains two adjacent words from the string. The list() function is used to convert the result to a list.

3. Convert String to List of Tuples Using a List Comprehension

You can also use list comprehension to convert a string to a list of tuples in Python. For example, the list comprehension iterates over the indices of the even-numbered words in the list of words obtained by splitting the string. For each even-indexed word, a tuple is created that contains that word and the following odd-indexed word. The resulting list of tuples is stored in the result variable.

# initialize string
string = “Spark By Examples .com is the Best Tutorial”
print(“Original String: “,string)

# Convert String to list of tuples
# Using list comprehension + zip()
temp = string.split()
result = [(temp[i], temp[i+1]) for i in range(0, len(temp), 2)]
print(“Result: “,result)

# Using list comprehension + zip()
result = [(string.split()[i], string.split()[i+1]) for i in range(0, len(string.split())-1, 2)]
print(“Result: “,result)

Yields the same output as above.

4. Using re.findall() and List Comprehension

You can use the re.findall() function and list comprehension to convert a string to a list of tuples in Python. For example, the re.findall() function is used with a regular expression pattern that matches adjacent pairs of words in the string. The pattern b(w+)b b(w+)b looks for two words separated by a space character, and the parentheses around w+ capture each word as a separate group.

The re.findall() function returns a list of tuples, where each tuple contains the two captured words from the string.

import re

# Initialize string
string = “Spark By Examples .com is the Best Tutorial”
print(“Original string: “,string)

# Convert String to list of tuples
# Using re.findall() and list comprehension
matches = re.findall(r’b(w+)b b(w+)b’, string)
result = [(match[0], match[1]) for match in matches]
print(“Convert string to list of tuples: “,result)

Yields the same output as above.

5. Using iter() + split() + next() + Generator Expression

Similarly, you can also use iter(), split(), next(), and a generator expression to convert a string into a list of tuples where each tuple contains two consecutive words from the string. For example,

# initialize string
string = “Spark By Examples .com is the Best Tutorial”
print(“Original string: “,string)

# Using iter() + split() + next() + generator expression
temp = iter(string.split())
result = [(element, next(temp)) for element in temp]
print(“Convert string to list of tuples: “,result)

Yields the same output as above.

Conclusion

In this article, I have explained how to convert a string to a list of tuples in python using several ways. The simple approach of converting a string to a list of tuples would be, first, you need to split the string by a delimiter and slice the list into two lists by selecting every other element and zip the two lists to create tuples, and finally convert them to list.

Happy Learning !!

 How to convert a string to a list of tuples in python? To convert a string to a list of tuples, you should split the string into individual elements and then group those elements into tuples. You need to specify the format of the string and the pattern to extract the values for each tuple.  Read More Python, Python Tutorial, python tuple examples 

Leave a Reply

Your email address will not be published. Required fields are marked *