NiRvanA
NiRvanA

Reputation: 135

How to access distinct elements of a tuple inside an array, passed as function argument?

I am trying to manipulate distinct elementf of a tuple, which is part of an array.

What I have:

def my_function(lis):
  for i in lis:
    x[i], y[i], z[i] = lis[i]
  ...

As in the main I have:

my_function([(1,2,3), (4,5,6), (7,8,9), (10,11,12)]):
  ...

The result was:

TypeError: list indices must be integers or slices, not tuple

As mentioned, I am trying to access distinc element of the tuple from the function and manipulate them.

Upvotes: 0

Views: 64

Answers (1)

chepner
chepner

Reputation: 532408

Each i is a tuple; you probably want

for i in lis:
    x, y, z = i
    # use x, y, and z

or simply

for x, y, z in lis:
    # use x, y, and z

Upvotes: 4

Related Questions