Convert List to Tuple in Python

Convert list to tuple in Python

It should work fine. Don't use tuple, list or other special names as a variable name. It's probably what's causing your problem.

>>> l = [4,5,6]
>>> tuple(l)
(4, 5, 6)

>>> tuple = 'whoops' # Don't do this
>>> tuple(l)
TypeError: 'tuple' object is not callable

Converting list to tuple in Python

Do not name variables after classes. In your example, you do this both with list and tuple.

You can rewrite as follows:

lst = ['a', 'b']
tup = tuple(lst)
lst.append('a')
another_tuple = tuple(lst)

Explanation by line

  1. Create a list, which is a mutable object, of 2 items.
  2. Convert the list to a tuple, which is an immutable object, and assign to a new variable.
  3. Take the original list and append an item, so the original list now has 3 items.
  4. Create a tuple from your new list, returning a tuple of 3 items.

The code you posted does not work as you intend because:

  • When you call another_tuple=tuple(list), Python attempts to treat your tuple created in the second line as a function.
  • A tuple variable is not callable.
  • Therefore, Python exits with TypeError: 'tuple' object is not callable.

How to perfectly convert one-element list to tuple in Python?

This is such a common question that the Python Wiki has a page dedicated to it:

One Element Tuples

One-element tuples look like:

1,

The essential element here is the trailing comma. As for any
expression, parentheses are optional, so you may also write
one-element tuples like

(1,)

but it is the comma, not the parentheses, that define the tuple.

How to convert a list to a list of tuples?

>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
# Create an iterator
>>> it = iter(L)
# zip the iterator with itself
>>> zip(it, it)
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]

You want to group three items at a time?

>>> zip(it, it, it)

You want to group N items at a time?

# Create N copies of the same iterator
it = [iter(L)] * N
# Unpack the copies of the iterator, and pass them as parameters to zip
>>> zip(*it)

Convert list into list of tuples of every two elements

Fun with iter:

it = iter(l)
[*zip(it, it)] # list(zip(it, it))
# [(0, 1), (2, 3), (4, 5)]

You can also slice in strides of 2 and zip:

[*zip(l[::2], l[1::2]))]
# [(0, 1), (2, 3), (4, 5)]

Convert list of strings to list of 2-tuples of integers (& Comma seperated in Tuple)

You can use regex to catch every integer from items of list, then form tuple from them as follows:

import re
ls= ['[11. 5.]', '[ 3. 16.]', '[ 0. 25.]', '[ 2. 40.]']

n = re.findall(r'\d+', ''.join(ls))
# -> ['11', '5', '3', '16', '0', '25', '2', '40']

res = [tuple(map(int, [n[i], n[i + 1]])) for i in range(0, len(n) - 1, 2)]
# -> [(11, 5), (3, 16), (0, 25), (2, 40)]

Convert list values from float to int in python

You can use list-comprehension, each element inside the list is tuple, so you need to take them into account individually, also you can use int() instead of round() to convert floating point number to integer:

[(int(x), int(y)) for x,y in lst]

[(22027, 22943), (22026, 22939), (22025, 22936), (22025, 22932), (22027, 22929), (22030, 22926), (22031, 22922), (22033, 22919), (22033, 22907), (22030, 22908), (22029, 22911), (22027, 22914), (22025, 22918), (22021, 22930), (22018, 22931), (22015, 22928), (22012, 22924), (22011, 22921), (22011, 22920)]

Convert tuple of list to list

Here it looks like you want the element inside the tuple, so if you do this:

myList = tupleA[0]

You'll get the list inside the tuple



Related Topics



Leave a reply



Submit