Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Using Packing and Unpacking for Efficient Code in Python | Mastering Packing and Unpacking in Python
Intermediate Python Techniques

bookUsing Packing and Unpacking for Efficient Code in Python

The most common usage of packing and unpacking can be seen in the following examples.

Values Swapping

Typically, to swap the values of variables, you might use an additional temporary variable:

a = 2
b = 3

temp = a  # temp = 2, a = 2, b = 3
a = b  # temp = 2, a = 3, b = 3
b = temp  # temp = 2, a = 3, b = 2

Simply put, unpacking enables you to swap values between variables in a more concise manner:

a, b = 2, 3

a, b = b, a # a = 3, b = 2

Dropping Unneeded Values

If you only need the first values from an iterable object, you can pass the unneeded values to the *_ variable.

my_list = ['Monica', 25, 'Doctor', 'Brazil']

name, age, *_ = my_list

Note

We use '_' single underscore for ignoring values: It's common to use _ as a throwaway variable to indicate that a specific value in a tuple unpacking is intentionally being ignored.

1. What is the value of 'c' after it is executed?

2. What does the following code snippet accomplish?

3. What is the result of the following Python code?

question mark

What is the value of 'c' after it is executed?

Select the correct answer

question mark

What does the following code snippet accomplish?

Select the correct answer

question mark

What is the result of the following Python code?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 3.7

bookUsing Packing and Unpacking for Efficient Code in Python

Scorri per mostrare il menu

The most common usage of packing and unpacking can be seen in the following examples.

Values Swapping

Typically, to swap the values of variables, you might use an additional temporary variable:

a = 2
b = 3

temp = a  # temp = 2, a = 2, b = 3
a = b  # temp = 2, a = 3, b = 3
b = temp  # temp = 2, a = 3, b = 2

Simply put, unpacking enables you to swap values between variables in a more concise manner:

a, b = 2, 3

a, b = b, a # a = 3, b = 2

Dropping Unneeded Values

If you only need the first values from an iterable object, you can pass the unneeded values to the *_ variable.

my_list = ['Monica', 25, 'Doctor', 'Brazil']

name, age, *_ = my_list

Note

We use '_' single underscore for ignoring values: It's common to use _ as a throwaway variable to indicate that a specific value in a tuple unpacking is intentionally being ignored.

1. What is the value of 'c' after it is executed?

2. What does the following code snippet accomplish?

3. What is the result of the following Python code?

question mark

What is the value of 'c' after it is executed?

Select the correct answer

question mark

What does the following code snippet accomplish?

Select the correct answer

question mark

What is the result of the following Python code?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3
some-alt