Python Program to Convert the List of Lists to a List of Tuples

List of Lists to List of Tuples

Write a program to convert the list of lists to a list of tuples.

Input

The first line of input will contain an integer (N), denoting the number of lists.
The next N lines will contain space-separated integers.

Output

The output should be a single line containing a list with N tuples in the order of inputs given.

Explanation

For example, if the given N is 3, read the inputs in the next three lines and print them as a list of tuples.

If the given three strings are as the following.

1 2 34
10 20 30
5 10 15 20

Your code should print each line of input as a tuple, such that making a list of three tuples. So the output should be

[(1, 2, 3, 4), (10, 20, 38), (5, 10, 15, 20)]

Sample Input
3
12 3 4
10 20 30
5 10 15 20

Sample Output
[(1, 2, 3, 4), (10, 20, 30), (5, 10, 15, 20)]

Code

def convert_nested_list_to_list_of_tuples(nested_list):
    tuples_list = []
    for each_list in nested_list:
        tuple_a = tuple(each_list)
        tuples_list.append(tuple_a)
    return tuples_list


def convert_string_to_int(list_a):
    new_list = []
    for item in list_a:
        num = int(item)
        new_list.append(num)
    return new_list


n = int(input())
num_list = []

for i in range(n):
    list_a = input().split()
    list_a = convert_string_to_int(list_a)
    num_list.append(list_a)


tuples_list = convert_nested_list_to_list_of_tuples(num_list)
print(tuples_list)

Input
4
-50 20 3
88 17
3 11
200 1800

Output

[(-50, 20, 3), (88, 17), (3, 11), (200, 1800)]

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published.