How do you convert a list of strings to a list of tuples?

Python | Convert list of strings to list of tuples

Sometimes we deal with different types of data types and we require to inter convert from one data type to another and hence inter conversion always a useful tool to have knowledge about. The interconversion from tuples to other formats have been discussed earlier. This article deals with the converse case. Let’s discuss certain ways in which this can be done.

Method #1 : Using map() + split() + tuple()

This task can be achieved using the combination of these functions. The map function can be used to link the logic to each string, split function is used to split the inner contents of list to different tuple attributes and tuple function performs the task of forming a tuple.




# Python3 code to demonstrate
# convert list of strings to list of tuples
# Using map() + split() + tuple()
# initializing list
test_list = ['4, 1', '3, 2', '5, 3']
# printing original list
print("The original list : " + str(test_list))
# using map() + split() + tuple()
# convert list of strings to list of tuples
res = [tuple(map(int, sub.split(', '))) for sub in test_list]
# print result
print("The list after conversion to tuple list : " + str(res))
Output : The original list : ['4, 1', '3, 2', '5, 3'] The list after conversion to tuple list : [(4, 1), (3, 2), (5, 3)]

Method #2 : Using map() + eval
This is the most elegant way to perform this particular task. Where map function is used to extend the function logic to the whole list eval function internally performs interconversions and splitting.




# Python3 code to demonstrate
# convert list of strings to list of tuples
# Using map() + eval
# initializing list
test_list = ['4, 1', '3, 2', '5, 3']
# printing original list
print("The original list : " + str(test_list))
# using map() + eval
# convert list of strings to list of tuples
res = list(map(eval, test_list))
# print result
print("The list after conversion to tuple list : " + str(res))
Output : The original list : ['4, 1', '3, 2', '5, 3'] The list after conversion to tuple list : [(4, 1), (3, 2), (5, 3)]

How do you convert a list of strings to a list of tuples?




Article Tags :
Python
Python Programs
Python list-programs

Convert list of strings to list of tuples in Python

PythonServer Side ProgrammingProgramming

While manipulating data with python, we may come across a list which has numbers as strings. Further we may want to convert the list of strings into tuples. Of course the given strings are in a particular format of numbers.

Method 1: List Comprehension + tuple()

Problem: How to convert a list of lists into a list of tuples?

Example: You’ve got a list of lists [[1, 2], [3, 4], [5, 6]] and you want to convert it into a list of tuples [(1, 2), (3, 4), (5, 6)].

How do you convert a list of strings to a list of tuples?

Solution: There are different solutions to convert a list of lists to a list of tuples. Use list comprehension in its most basic form:

lst = [[1, 2], [3, 4], [5, 6]] tuples = [tuple(x) for x in lst] print(tuples) # [(1, 2), (3, 4), (5, 6)]

Try It Yourself:

This approach is simple and effective. List comprehension defines how to convert each value (x in the example) to a new list element. As each list element is a new tuple, you use the constructor tuple(x) to create a new tuple from the list x.

If you have three list elements per sublist, you can use the same approach with the conversion:

lst = [[1, 2, 1], [3, 4, 3], [5, 6, 5]] tuples = [tuple(x) for x in lst] print(tuples) # [(1, 2, 1), (3, 4, 3), (5, 6, 5)]

You can see the execution flow in the following interactive visualization (just click the “Next” button to see what’s happening in the code):

And if you have a varying number of list elements per sublist, this approach still works beautifully:

lst = [[1], [2, 3, 4], [5, 6, 7, 8]] tuples = [tuple(x) for x in lst] print(tuples) # [(1,), (2, 3, 4), (5, 6, 7, 8)]

You see that an approach with list comprehension is the best way to convert a list of lists to a list of tuples. But are there any alternatives?

3 Ways to Convert List to Tuple in Python

How do you convert a list of strings to a list of tuples?

There may be times when you wish to have a specific typeof a variable during programming. Typecasting is a process used to change the variables declared in a specific data type into a different data type to match the operation performed by the code snippet. Python is an object-oriented language that consists of many data types. Today, we will learn List and Tuple data type in python and some methods to convert python list to a tuple in detail.

Python convert tuple to list

We can use the list() function to convert tuple to list in Python.

Example:

value = (10, 20, 30, 40, 50) my_tuple = list(value) print(my_tuple)

After writing the above code, Ones you will print” my_tuple ”then the output will appear as a“ [10, 20, 30, 40, 50] ”.

Here, thelist()function will convert the tuple to the list. Refer the screenshot for python convert tuple to list.

How do you convert a list of strings to a list of tuples?

In this way, we can convert a tuple to list in python.

Read Python string to list

Convert lists and tuples to each other in Python

Posted: 2020-07-22 / Tags: Python, List
Tweet

In Python, you can convert lists list and tuples tuple to each other by using list() and tuple().

For convenience, the word "convert" is used, but in reality, a new object is generated, and the original object remains the same.

list() and tuple() return new list and tuple when given an iterable object such as list, tuple, set, range, etc.

  • Built-in Functions - list() — Python 3.8.5 documentation
  • Built-in Functions - tuple() — Python 3.8.5 documentation

In the following sample code, list, tuple, and range type objects are used as examples.

l = [0, 1, 2] print(l) print(type(l)) # [0, 1, 2] # <class 'list'> t = ('one', 'two', 'three') print(t) print(type(t)) # ('one', 'two', 'three') # <class 'tuple'> r = range(10) print(r) print(type(r)) # range(0, 10) # <class 'range'>
source: list_tuple.py

See the following article for details on range().

  • How to use range() in Python
Sponsored Link