Created
February 23, 2018 23:46
-
-
Save sravi4701/8c41f7f0c09838950a9a1797d949d3f9 to your computer and use it in GitHub Desktop.
Unpacking elements from iterables of arbitrary length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grades = [5, 6, 7, 8, 9] | |
first, *middle, last = grades | |
print(first) # 5 | |
print(last) # 9 | |
print(middle) # [6, 7, 8] | |
# The variable associated with * will always be a list | |
# We also can't use two * expression like: | |
first, *middle, *last = grades # this will get an error | |
# More complex example | |
data = ('Ravi', 21, (5, 12, 1996)) | |
# We need only name and year from data | |
name, *_, (*_, year) = data | |
print(name) # Ravi | |
print(year) # 1996 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment