Is there a better algorithm?
Steve Holden
steve at holdenweb.com
Fri Jan 2 13:41:58 EST 2009
Kottiyath wrote:
> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
>
> I want to loop through the list and extract the values.
> The only algorithm I could think of is:
>>>> for i in l:
> ... u = None
> ... try:
> ... (k, v) = i
> ... except ValueError:
> ... (k, u, v) = i
> ... print k, u, v
> ---------
> 1 None 2
> 3 4 5
> 6 None 7
> -------------
> But, this algorithm doesnt look very beautiful - like say -> for k, u,
> v in L:
> Can anyone suggest a better algorithm to get the values?
L = [(1, 2), (3, 4, 5), (6, 7)]
for i in L:
if len(i) == 2:
k, v, u = i + (None, )
else:
k, u, v = i
print k, u, v
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list