Thursday 29 June 2023

Python: unpack the list to multiple variables

Unpacking is a procedure to assign values of an iterable to multiple values.

 

Example

primes = [2, 3, 5]
first_prime, second_prime, third_prime = primes

In the above example,

a.   first_prime is assigned with the value 2

b.   second_prime is assigned with the value 3

c.    third_prime is assigned with the value 5

 

unpack_data.py

primes = [2, 3, 5]
first_prime, second_prime, third_prime = primes

print('first_prime : ', first_prime)
print('second_prime : ', second_prime)
print('third_prime : ', third_prime)

Output

first_prime :  2
second_prime :  3
third_prime :  5

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment