chevron_left
Cookbooks
0
0
0
new
Unpacking in Python
Programming
chevron_rightPython
chevron_rightBuilt-in Functions
chevron_rightCookbooks
schedule May 18, 2022
Last updated Python
Tags tocTable of Contents
expand_more Unpacking in Python refers to assigning values in an iterable object to a tuple or list of variables for later use. We can perform unpacking by using an asterisk (*
) before the object we would like to unpack.
Unpacking a range object
To first create a range object of odd numbers between 1 and 10:
numbers = range(1,11,2)print(numbers)
range(1, 11, 2)
To unpack the elements of the range object into a list:
numbers_list = [*numbers]print(numbers_list)
[1, 3, 5, 7, 9]
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...