{"id":114924,"date":"2020-07-02T19:39:38","date_gmt":"2020-07-02T19:39:38","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=10378"},"modified":"2020-07-02T19:39:38","modified_gmt":"2020-07-02T19:39:38","slug":"python-tuple-to-integer","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/02\/python-tuple-to-integer\/","title":{"rendered":"Python Tuple to Integer"},"content":{"rendered":"<p>You have a tuple of integers&#8212;but you want a single integer. What can you do?<\/p>\n<p><strong>Problem<\/strong>: Given a tuple of values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">t = (1, 2, 3)<\/pre>\n<p>Your goal is to convert it to a single integer value.<\/p>\n<p>There are multiple ways of accomplishing this (dependent on what exactly you want to do). Let&#8217;s get a quick overview in our interactive Python shell:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/bf5faba68b\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Modify method 2 to calculate the average and round to the next integer!<\/em><\/p>\n<p>Let&#8217;s dive into each of the method.<\/p>\n<h2>Method 1: sum()<\/h2>\n<p>The first way of converting a tuple to an integer, simply sum up all values. The <code>sum()<\/code> function is built-in in Python and you can use it on any iterable:<\/p>\n<p><strong>The syntax is <code>sum(iterable, start=0)<\/code>:<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Argument<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>iterable<\/code><\/td>\n<td>Sum over all elements in the <code>iterable<\/code>. This can be a list, a tuple, a set, or any other data structure that allows you to iterate over the elements. <br \/><strong>Example<\/strong>: <code>sum([1, 2, 3])<\/code> returns <code>1+2+3=6<\/code>. <\/td>\n<\/tr>\n<tr>\n<td><code>start<\/code><\/td>\n<td>(Optional.) The default start value is 0. If you define another start value, the sum of all values in the <code>iterable<\/code> will be added to this start value. <br \/><strong>Example<\/strong>: <code>sum([1, 2, 3], 9)<\/code> returns <code>9+1+2+3=15<\/code>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python sum() List - A Simple Illustrated Guide\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/BGddaP8pNcc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>Here&#8217;s how you can use the <code>sum()<\/code> function to sum over all values in an iterable (such as a tuple):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n# Method 1: sum()\nt = (1, 2, 3)\ni = sum(t)\nprint(i)\n# 6<\/pre>\n<p>In this case, it calculates 1+2+3=6. You can learn more about the <code>sum()<\/code> function <a href=\"https:\/\/blog.finxter.com\/python-sum-list\/\" title=\"Python sum() List \u2013 A Simple Illustrated Guide\" target=\"_blank\" rel=\"noreferrer noopener\">on this Finxter blog article<\/a>.<\/p>\n<p>But what if you want to use all tuple values as digits of a larger integer value?<\/p>\n<h2>Method 2: str() + list comprehension + join()<\/h2>\n<p><a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"List Comprehension in Python \u2014 A Helpful Illustrated Guide\">List comprehension<\/a> is a compact way of creating lists. The simple formula is <code>[expression + context]<\/code>.<\/p>\n<ul>\n<li><strong>Expression<\/strong>: What to do with each list element?<\/li>\n<li><strong>Context<\/strong>: What elements to select? The context consists of an arbitrary number of <code>for<\/code> and <code>if<\/code> statements.<\/li>\n<\/ul>\n<p>You can use it in combination with the <code>sum()<\/code> function to calculate the integer 123 from the tuple (1, 2, 3)&#8212;by using the tuple values as digits of the larger integer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n# Method 2: str() + list comprehension + join()\nt = (1, 2, 3)\ni = ''.join(str(x) for x in t)\nprint(int(i))\n# 123<\/pre>\n<p>Well, to be frank, we didn&#8217;t even use list comprehension here&#8212;the correct term for <code>str(x) for x in t<\/code> is &#8220;generator expression&#8221;. The difference to list comprehension is that it creates a generator instead of a list. <\/p>\n<p>If you like functional programming, you may like the following method: <\/p>\n<h2>Method 3: str() + map() + join()<\/h2>\n<p>The map() function creates a new iterable from an iterable by applying a function to each element of the original iterable:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Mastering the Python Map Function [+Video]\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/tqph6mWC3m8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>You can pass the <code><a href=\"https:\/\/blog.finxter.com\/python-list-to-string\/\" title=\"Python List to String: A Helpful Guide with Interactive Shell\">str()<\/a><\/code> function into the <code><a href=\"https:\/\/blog.finxter.com\/how-to-get-rid-of-pythons-map-function-with-list-comprehension\/\" title=\"How to Get Rid of Python\u2019s Map Function With List Comprehension\">map()<\/a><\/code> function to convert each tuple element to a string.<\/p>\n<p>Then, you can <a href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Join List [Ultimate Guide]\">join <\/a>all strings together to a big string. After converting the big string to an integer, you&#8217;ve successfully merged all tuple integers to a big integer value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n# Method 3: str() + map() + join()\nt = (1, 2, 3)\ni = ''.join(map(str, t))\nprint(i)\n# 123<\/pre>\n<p>There are many details to the <code>string.join()<\/code> method. <a href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Join List [Ultimate Guide]\">You can read the detailed tutorial on the Finxter blog.<\/a> Here&#8217;s the short version:<\/p>\n<p>The <code>string.join(iterable)<\/code> method concatenates all the string elements in the <code>iterable<\/code> (such as a list, string, or tuple) and returns the result as a new string. The <code>string<\/code> on which you call it is the delimiter string\u2014and it separates the individual elements. For example, <code>'-'.join(['hello', 'world'])<\/code> returns the joined string <code>'hello-world'<\/code>.<\/p>\n<h2>Method 4: Multiple Assignments<\/h2>\n<p>If you simply want to get multiple integers by assigning the individual tuple values to integer variables, just use the <a href=\"https:\/\/blog.finxter.com\/the-top-18-best-python-tricks\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Top 18 Cool Python Tricks\">multiple assignment<\/a> feature:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n# Method 4: multiple assignments\nt = (1, 2, 3)\na, b, c = t\nprint(a)\nprint(b)\nprint(c) '''\n1\n2\n3 '''\n<\/pre>\n<p>Variables <code>a<\/code>, <code>b<\/code>, and <code>c<\/code> have the values 1, 2, and 3, respectively.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have a tuple of integers&#8212;but you want a single integer. What can you do? Problem: Given a tuple of values. t = (1, 2, 3) Your goal is to convert it to a single integer value. There are multiple ways of accomplishing this (dependent on what exactly you want to do). Let&#8217;s get a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-114924","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/114924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=114924"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/114924\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=114924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=114924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=114924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}