amchew
amchew

Reputation: 1187

python - how do you store elements in a tuple

how to store elements in a tuple?

I did this:

for i in range (0, capacity):
    for elements in self.table[i]:
        # STORE THE ALL THE ELEMENTS IN THE TUPLE

Upvotes: 1

Views: 11800

Answers (4)

John La Rooy
John La Rooy

Reputation: 304463

Since you haven't told us - we can only guess what table looks like If it is a list of lists for example, you can do this to get a tuple of tuples

>>> table =[[1,2,3],[4,5,6],[7,8,9]]
>>> tuple(map(tuple, table))
((1, 2, 3), (4, 5, 6), (7, 8, 9))

>>> capacity=2
>>> tuple(map(tuple, table[:capacity]))
((1, 2, 3), (4, 5, 6))

Upvotes: 1

Senthil Kumaran
Senthil Kumaran

Reputation: 56951

tuples are immutable. You can create a tuple. But you cannot store elements to an already created tuple. To create (or convert) your list of elements to a tuple. Just tuple() it.

t = tuple([1,2,3])

In your case it is

t = tuple(self.table[:capacity])

Upvotes: 7

Patrick
Patrick

Reputation: 92620

I think this is what you want.

x = []
for i in range (0, capacity):
  for elements in self.table[i]:
    x.append(elements)
t = tuple(x)

Upvotes: -1

user470379
user470379

Reputation: 4887

It's as easy as t = tuple(self.table[i])

Upvotes: 0

Related Questions