Convert list of tuples to int python

You didn't say what you mean by "best", but presumably you mean "most pythonic" or "most readable" or something like that.

The list comprehension given by F3AR3DLEGEND is probably the simplest. Anyone who knows how to read a list comprehension will immediately know what it means.

y = [i[0] for i in x]

However, often you don't really need a list, just something that can be iterated over once. If you've got a billion elements in x, building a billion-element y just to iterate over it one element at a time may be a bad idea. So, you can use a generator expression:

y = (i[0] for i in x)

If you prefer functional programming, you might prefer to use map. The downside of map is that you have to pass it a function, not just an expression, which means you either need to use a lambda function, or itemgetter:

y = map(operator.itemgetter(0), x)

In Python 3, this is equivalent to the generator expression; if you want a list, pass it to list. In Python 2, it returns a list; if you want an iterator, use itertools.imap instead of map.

If you want a more generic flattening solution, you can write one yourself, but it's always worth looking at itertools for generic solutions of this kind, and there is in fact a recipe called flatten that's used to "Flatten one level of nesting". So, copy and paste that into your code (or pip install more-itertools) and you can just do this:

y = flatten(x)

If you look at how flatten is implemented, and then at how chain.from_iterable is implemented, and then at how chain is implemented, you'll notice that you could write the same thing in terms of builtins. But why bother, when flatten is going to be more readable and obvious?

Finally, if you want to reduce the generic version to a nested list comprehension (or generator expression, of course):

y = [j for i in x for j in i]

However, nested list comprehensions are very easy to get wrong, both in writing and reading. (Note that F3AR3DLEGEND, the same person who gave the simplest answer first, also gave a nested comprehension and got it wrong. If he can't pull it off, are you sure you want to try?) For really simple cases, they're not too bad, but still, I think flatten is a lot easier to read.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Let’s discuss certain ways in which this task is performed. Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of list is by using re. 

    Python3

    import re

    lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]

    temp = re.sub(r'[\[\]\(\), ]', '', str(lst))

    Output = [int(i) for i in set(temp)]

    print("Initial List is :", lst)

    print("Output list is :", Output)

    Output:

    Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)] Output list is : [1, 4, 8, 0, 3, 2]

      Method #2: Using itertools.chain() and lambda() This is yet another way to perform this particular task using lambda(). 

    Python3

    from itertools import chain

    lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]

    temp = map(lambda x: str(x), chain.from_iterable(lst))

    Output = set()

    for x in temp:

        for elem in x:

            Output.add(elem)

    print("Initial List is :", lst)

    print("Output list is :", Output)

    Output:

    Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)] Output list is : {‘8’, ‘4’, ‘0’, ‘2’, ‘1’, ‘3’}

    Method #3: Using list(),map(),join() and set() methods

    Initially we convert list containing tuple of integers to list containing tuple of strings.Later we will concatenate the string(obtained by joining tuple strings) to an empty string.And then use set to remove duplicates.Finally convert them to integer type and print output list

    Python3

    lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]

    p=""

    for i in lst:

        x=list(map(str,i))

        p+="".join(x)

    p=list(map(int,set(p)))

    print("Initial List is :", lst)

    print("Output list is :", p)

    Output

    Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
    Output list is : [0, 4, 2, 3, 8, 1]


    How do you convert tuples to integers?

    There are multiple ways to convert a tuple to an integer: Access a tuple element at its index and convert it to an int, e.g. int(my_tuple[0]) . Sum or multiply the elements of the tuple. Convert a tuple of strings to a tuple of integers.

    How do you convert a list of tuples?

    Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.

    How do you convert a list to an int in Python?

    Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.

    How do you convert a tuple to a list of tuples in Python?

    Method #1 : Using loop + * operator + items() This is one of the ways in which this task can be performed. In this we iterate for all the keys using loop and map keys with all values by unpacking all values in tuple using * operator.