My Coding >
Programming language >
Python >
Python FAQ >
Python: How to unpack the beginning of the list
Python: How to unpack the beginning of the listIf you have a list or tuple, to unpack it you can use the following expression:
The number of variables in the left part should be equal to the number of elements in the right part, otherwise, you will have an error: ValueError: too many values to unpack (expected 2) But what should you do if you want to take only a few first values? Use star!!!
In this case, a and b will be 1 and 2, respectively, and all outstanding parts of the list will go to the new list rest
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |