[Tutor] Adding index numbers to tuple

Martin A. Brown martin at linux-ip.net
Tue Aug 16 15:17:12 CEST 2011


Hello,

 : Maybe a bit confusing topic title, probably the example will do.
 : 
 : I have a tuple:
 : t = ('a', 'b', 'c', 'd')
 : And need the following output, list or tuple, doesn't matter:
 : (0, 'a', 1, 'b', 2, 'c', 3, 'd')
 : 
 : I tried with zip(), but get a list of tuples, which isn't the desired
 : output. Anyone with a solution or push in the right direction?

Perhaps you did not know about enumerate?

  t = ('a', 'b', 'c', 'd')
  l = list()
  for x in enumerate(t):
      l.extend(x)

-Martin

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list