Skip to content

Instantly share code, notes, and snippets.

@sravi4701
Created February 23, 2018 23:46
Show Gist options
  • Save sravi4701/8c41f7f0c09838950a9a1797d949d3f9 to your computer and use it in GitHub Desktop.
Save sravi4701/8c41f7f0c09838950a9a1797d949d3f9 to your computer and use it in GitHub Desktop.
Unpacking elements from iterables of arbitrary length
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