Python built-in types — Tuples
Tuple is a python built-in data structure which holds a sequence of values, and the values can be in any data type. If you write a hundred lines of python code, it is almost impossible to avoid it in your code, as it comes in implicitly or explicitly from your variable assignment and iteration to return values of your method. In this article, I will be sharing with you where and how the tuples will be possibly used in your code.
Variable assignment
You may have written the code in the below way to assign the values to variables in one line. The left side is the tuple of variables, and the right side is the tuple of values/expressions.
sort_by_name, sort_by_date = True, False
#output : sort_by_name True, sort_by_date False
key, val = "20200601" , "Mon"
#output : key '20200601', val 'Mon'
Sometimes if you want to swap the values of two variables, you do not need to create a temp variable for swapping. The below will do a perfect job to swap the value for key, val variables.
key, val = val, key
#output: key 'Mon', val '20200601'
Traverse the elements of a sequence
If you want to iterate through each of the elements in a sequence and meanwhile get the index of the element, you can do it by below code:
for idx, label in enumerate('ABCDEFG'):
print(idx, label)#output: 0, A
#1, B
#...
When iterating a dictionary, the iterms method returns a list of tuples, and each tuple is the key and value pair, e.g.:
company_info = {"name" : "Alibaba", "headquarter" : "Hangzhou, China", "founded" : "4 April 1999"}
for key, val in company_info.items():
print(f"{key} : {val}")
If you have checked my another post — How to swap the key and value in a python dictionary, it is just an extension to the above.
Iterate multiple sequences at one time with zip
If you use the built-in zip function to iterate multiple sequences at one time, it actually returns an iterator of tuples. See the below example:
names = ["Alibaba", "Amazon", "Google"]
countries = ["China", "USA", "USA"]
years = ["1999", "1996", "1998"]
for rec in zip(names, countries, years):
print(rec)#output:
#('Alibaba', 'China', '1999')
#('Amazon', 'USA', '1996')
#('Google', 'USA', '1998')
Return multiple values from function
Normally a function can only returns 1 value, but with tuple, you can return multiple values even in different data types. (technically speaking, it is still 1 value but tuple type)
e.g. The python built-in method divmod:
quotient, remainder = divmod(10, 3)
print(quotient, remainder)
#output: 3 1
You can also define your own function to return multiple values like below:
def split_email(email):
user_name, company_site = email.split("@")
return user_name, company_site
split_email("contact@codeforests.com")
#output: ('contact', 'codeforests.com')
With above example, I am going to wrap up my article for this topic. If you have any questions or comments, please share in the below.
Link of the original post at: https://www.codeforests.com/2020/06/28/python-built-in-types-tuples/