Universally unpacking tuples

Peter Bittner peter.bittner at gmx.net
Mon Sep 23 02:33:04 EDT 2002

Hi there!

I have problems universally unpacking tuples. I would like to take the
first element of a tuple and remove it (thus creating a new tuple with
one element less). Something like in the following example:

arbitraryTuple = ('abc', '123', 'qwert', ...)
while len(arbitraryTuple) > 0:
  elem, arbitraryTuple = arbitraryTuple
  print elem

Well, I know this doesn't work since unpacking requires the correct
number of elements on the left side... :-(

The root of the problem is that I am forced to move from a
list-solution (iterating over the elements of a list) to a
tuple-solution (iterating over the elements of a tuple). So, what I am
trying to do with the above code is nothing more than migrating the
following list-oriented code to a tuple-thing:

mylist = ['abc', '123', 'qwert', ...]
for i in range(len(mylist)):
  elem = mylist[i]
  print elem

Does anyone know a simple solution to this problem?
Cheers, Peter

More information about the Python-list mailing list