Intermediate Python Knowledge

How to Pack and Unpack Data in Python — Tuples and Dictionaries

Understand the magic asterisks (*)

Yong Cui
Yong Cui
May 8, 2020 · 5 min read
Photo by Ryan Hutton on Unsplash
>>> # Create a tuple
>>> employee = ('John', 35, 'Male')
>>>
>>> # Create a dictionary
>>> grades = {'John': 95, 'Jennifer': 92, 'Aaron': 98}

The Asterisk (*) Operator

>>> # Multiplication with *
>>> 3 * 2
6
>>> # Exponentiation with **
>>> 3 ** 3
27

Tuple Packing and Unpacking

>>> # Create a tuple of prime numbers 
>>> primes = (2, 3, 5, 7, 11, 13)
>>>
>>> # Access elements using indexing
>>> primes[0]
2
>>> # Access elements using an indexing range
>>> primes[1:4]
(3, 5, 7)
>>> # Access elements using reverse index
>>> primes[-1]
13
>>> # Create a tuple of prime numbers
>>> primes = (2, 3, 5, 7, 11, 13)
>>>
>>> # Unpack the tuple, individually
>>> a, b, c, d, e, f = primes
>>> print(a, b, c, d, e, f, sep='||')
2||3||5||7||11||13
>>>
>>> # Unpack the tuple, using the asterisk
>>> p0, *p1, p2 = primes
>>> print(p0, p1, p2, sep='||')
2||[3, 5, 7, 11]||13
>>>
>>> # Check the type of p1
>>> type(p1)
<class 'list'>
>>> # Define a function with *args
>>> def show_countries(*args):
... print(f"The args type: {type(args)}")
... for item in args:
... print(item)
...
>>> # Call the function
>>> show_countries('America', 'Canada')
The args type: <class 'tuple'>
America
Canada

Dictionary Packing and Unpacking

>>> # Define a function with **kwargs
>>> def show_populations(**kwargs):
... print(f"The kwargs type: {type(kwargs)}")
... for key, value in kwargs.items():
... print(f"{key}: {value}")
...
>>> # Call the function
>>> show_populations(America='328.2 million', Canada='37.59 million')
The kwargs type: <class 'dict'>
America: 328.2 million
Canada: 37.59 million
>>> # Declare a dictionary
>>> grades0 = {'John': 95, 'Jennifer': 98}
>>>
>>> # Dictionary Unpacking
>>> grades1 = {**grades0}
>>>
>>> # Aliasing
>>> grades2 = grades0
>>>
>>> # Check the identities
>>> grades1 is grades0
False
>>> grades2 is grades0
True
>>> # Create two dictionaries 
>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d2a = {'b': 20, 'c': 3, 'd':4}
>>>
>>> # Merge two dictionaries
>>> d_merged0 = {**d1, **d2}
>>> d_merged0
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>>
>>> # Merge two dictionaries having an overlapping key
>>> d_merged1 = {**d1, **d2a}
>>> d_merged1
{'a': 1, 'b': 20, 'c': 3, 'd': 4}

Conclusions

The Startup

Get smarter at building your thing. Join The Startup’s +787K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Yong Cui

Written by

Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Yong Cui

Written by

Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app