Amila
Amila

Reputation: 63

How is this happening? python

my_list= [(0,1,2),(3,4,5),(6,7,8)]
for a,b,c in my_list:
    print(a)
    print(b)
    print(c)
my_list = a+b+c
print(my_list) 

I run this and it comes as 21 for last i would just like to know how that comes thanks again!

Upvotes: 0

Views: 63

Answers (2)

IamFr0ssT
IamFr0ssT

Reputation: 769

What you are doing is iterating through your array, and unpacking the tuple into a,b,c.

The last tuple is (6,7,8) and when it maps to (a,b,c) it is unpacked to a=6, b=6, c=8.

What you call my_list will not be a list, but the sum of a,b and c.

Upvotes: 0

adshin21
adshin21

Reputation: 188

In the last iteration a = 6, b = 7 and c = 8, hence it is giving you 21.

Upvotes: 1

Related Questions