{"id":125940,"date":"2022-06-23T05:52:34","date_gmt":"2022-06-23T05:52:34","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=435162"},"modified":"2022-06-23T05:52:34","modified_gmt":"2022-06-23T05:52:34","slug":"convert-csv-to-list-of-tuples-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/06\/23\/convert-csv-to-list-of-tuples-in-python\/","title":{"rendered":"Convert CSV to List of Tuples in Python"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;435162&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;2&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4.5\\\/5 - (2 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;128&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 128px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> 4.5\/5 &#8211; (2 votes) <\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/python_csv_to_list_of_tuples-1024x576.jpg\" alt=\"Convert CSV to List of Tuples in Python\" class=\"wp-image-435167\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/python_csv_to_list_of_tuples-1024x576.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/python_csv_to_list_of_tuples-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/python_csv_to_list_of_tuples-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/python_csv_to_list_of_tuples.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<p>Given a CSV file (e.g., stored in the file with name <code>'my_file.csv'<\/code>). <\/p>\n<pre class=\"wp-block-preformatted\"><code><strong>INPUT<\/strong>: file 'my_file.csv'<\/code>\n<code>9,8,7\n6,5,4\n3,2,1<\/code><\/pre>\n<p><strong>Challenge<\/strong>: How to convert the CSV file to a list of tuples, i.e., putting the row values into the inner <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"post\" data-id=\"12043\" target=\"_blank\" rel=\"noreferrer noopener\">tuples<\/a>?<\/p>\n<pre class=\"wp-block-preformatted\"><code><strong>OUTPUT<\/strong>: Python list of tuples<\/code>\n<code>[(9, 8, 7), (6, 5, 4), (3, 2, 1)]<\/code><\/pre>\n<h2>Method 1: csv.reader()<\/h2>\n<div class=\"wp-block-cover aligncenter\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-80 has-background-dim\"><\/span><img decoding=\"async\" loading=\"lazy\" width=\"642\" height=\"642\" class=\"wp-block-cover__image-background wp-image-426802\" alt=\"\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png\" data-object-fit=\"cover\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png 642w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-300x300.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-150x150.png 150w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-base-3-color has-text-color has-x-large-font-size\"><strong>Method 1: csv.reader()<\/strong><\/p>\n<\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\">To convert a CSV file <code>'my_file.csv'<\/code> into a list of tuples in Python, use <code>csv.reader(file_obj)<\/code> to create a CSV file reader that holds an iterable of lists, one per row. Now, use the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list\/\" target=\"_blank\"><code>list(tuple(line) for line in reader)<\/code><\/a> expression with a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/understanding-generators-in-python\/\" data-type=\"post\" data-id=\"33873\" target=\"_blank\">generator expression<\/a> to convert each inner list to a tuple.<\/p>\n<p>Here&#8217;s a simple example that converts our CSV file to a nested list using this approach:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(tuple(line) for line in reader)<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(lst)\n# [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]<\/pre>\n<h2>Method 2: One-Liner<\/h2>\n<div class=\"wp-block-cover aligncenter\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-80 has-background-dim\"><\/span><img decoding=\"async\" loading=\"lazy\" width=\"642\" height=\"642\" class=\"wp-block-cover__image-background wp-image-426802\" alt=\"\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png\" data-object-fit=\"cover\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png 642w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-300x300.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-150x150.png 150w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-base-3-color has-text-color has-x-large-font-size\"><strong>Method 2: One-Liner<\/strong><\/p>\n<\/div>\n<\/div>\n<p class=\"has-base-3-background-color has-background\">You can also convert a CSV to a list of tuples using the following <a rel=\"noreferrer noopener\" href=\"https:\/\/pythononeliners.com\/\" data-type=\"URL\" data-id=\"https:\/\/pythononeliners.com\/\" target=\"_blank\">Python one-liner<\/a> idea:<\/p>\n<p class=\"has-global-color-8-background-color has-background\">Open the file using <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-open-function\/\" data-type=\"post\" data-id=\"24793\" target=\"_blank\">open()<\/a><\/code>, pass the file object into <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-read-a-csv-file-into-a-python-list\/\" data-type=\"post\" data-id=\"8185\" target=\"_blank\">csv.reader()<\/a><\/code>, and convert the CSV reader object to a list using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list\/\" data-type=\"post\" data-id=\"21502\" target=\"_blank\">list()<\/a><\/code> built-in function in Python with a <a href=\"https:\/\/blog.finxter.com\/python-one-line-generator\/\" data-type=\"post\" data-id=\"13194\" target=\"_blank\" rel=\"noreferrer noopener\">generator expression<\/a> to convert each inner list to a tuple.<\/p>\n<p>Here&#8217;s how that looks:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import csv; lst=list(tuple(line) for line in csv.reader(open('my_file.csv'))); print(lst)<\/pre>\n<p>Concise, isn&#8217;t it? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>The output is the same:<\/p>\n<pre class=\"wp-block-preformatted\"><code>[('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]<\/code><\/pre>\n<p>By the way: <em>Do you need all inner elements converted to a certain data type such as integer?<\/em><\/p>\n<p>If so, use the following approach on top of the previously shown list <code>lst<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new_lst = [tuple(int(x) for x in inner) for inner in lst]<\/pre>\n<p>The converted <code>new_lst<\/code> now contains a list of tuples of ints:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(new_lst)\n# [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]<\/pre>\n<h2>Method 3: Pandas<\/h2>\n<div class=\"wp-block-cover aligncenter\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-80 has-background-dim\"><\/span><img decoding=\"async\" loading=\"lazy\" width=\"642\" height=\"642\" class=\"wp-block-cover__image-background wp-image-426802\" alt=\"\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png\" data-object-fit=\"cover\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png 642w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-300x300.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-150x150.png 150w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-base-3-color has-text-color has-x-large-font-size\"><strong>Method 3: Pandas<\/strong><\/p>\n<\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\">You can convert a CSV to a list of tuples with Pandas by first <a href=\"https:\/\/blog.finxter.com\/read-and-write-flat-files-with-pandas\/\" data-type=\"post\" data-id=\"62847\">readin<\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/read-and-write-flat-files-with-pandas\/\" data-type=\"post\" data-id=\"62847\" target=\"_blank\">g<\/a><a href=\"https:\/\/blog.finxter.com\/read-and-write-flat-files-with-pandas\/\" data-type=\"post\" data-id=\"62847\"> the CSV<\/a> without header line using <code>pd.read_csv('my_file.csv', header=None)<\/code> function and second converting the resulting DataFrame to a nested list using <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/dataframe-to-a-list-and-vice-versa\/\" data-type=\"post\" data-id=\"111548\" target=\"_blank\">df.values.tolist()<\/a><\/code>. Third, <a href=\"https:\/\/blog.finxter.com\/how-to-convert-list-of-lists-to-list-of-tuples-in-python\/\" data-type=\"post\" data-id=\"7953\" target=\"_blank\" rel=\"noreferrer noopener\">convert<\/a> the nested list to a list of tuples and you&#8217;re done.<\/p>\n<p>Here&#8217;s an example that converts the CSV to a Pandas DataFrame and then to a nested raw Python list and then to a list of tuples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas as pd # CSV to DataFrame\ndf = pd.read_csv('my_file.csv', header=None) # DataFrame to List of Lists\nlst = df.values.tolist() # List of Lists to List of Tuples:\nnew_lst = [tuple(x) for x in lst] print(new_lst)\n# [(9, 8, 7), (6, 5, 4), (3, 2, 1)]<\/pre>\n<p>This was easy, wasn&#8217;t it? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Of course, you can also one-linerize it by chaining commands like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># One-Liner to convert CSV to list of tuples:\nlst = [tuple(x) for x in pd.read_csv('my_file.csv', header=None).values.tolist()]<\/pre>\n<h2>Method 4: Raw Python No Dependency<\/h2>\n<div class=\"wp-block-cover aligncenter\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-80 has-background-dim\"><\/span><img decoding=\"async\" loading=\"lazy\" width=\"642\" height=\"642\" class=\"wp-block-cover__image-background wp-image-426802\" alt=\"\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png\" data-object-fit=\"cover\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png 642w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-300x300.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-150x150.png 150w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-base-3-color has-text-color has-x-large-font-size\"><strong>Method 4: Raw Python No Dependency<\/strong><\/p>\n<\/div>\n<\/div>\n<p>If you&#8217;re like me, you try to avoid using dependencies if they are not needed. Raw Python is often more efficient and simple enough anyways. Also, you don&#8217;t open yourself up to unnecessary risks and complexities.<\/p>\n<p class=\"has-base-background-color has-background\"><strong>Question<\/strong>: So, is there a simple way to read a CSV to a list of tuples in raw Python without external dependencies? <\/p>\n<p>Sure!<\/p>\n<p class=\"has-global-color-8-background-color has-background\">To read a CSV to a list of tuples in pure Python, open the file using <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-open-function\/\" data-type=\"post\" data-id=\"24793\" target=\"_blank\">open('my_file.csv')<\/a><\/code>, read all lines into a variable using <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-read-first-n-lines-of-a-file-in-python\/\" data-type=\"post\" data-id=\"30520\" target=\"_blank\">f.readlines()<\/a><\/code>. Iterate over all lines, strip them from whitespace using <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-string-strip\/\" data-type=\"post\" data-id=\"26104\" target=\"_blank\">strip()<\/a><\/code>, split them on the delimiter <code>','<\/code> using <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-string-split\/\" data-type=\"post\" data-id=\"26097\" target=\"_blank\">split(',')<\/a><\/code>, and pass everything in the <code><a href=\"https:\/\/blog.finxter.com\/python-tuple\/\" data-type=\"post\" data-id=\"21575\" target=\"_blank\" rel=\"noreferrer noopener\">tuple()<\/a><\/code> function.<\/p>\n<p>You can accomplish this in a simple <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\" target=\"_blank\" rel=\"noreferrer noopener\">list comprehension<\/a> statement like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">csv_filename = 'my_file.csv' with open(csv_filename) as f: lines = f.readlines() lst = [tuple(line.strip().split(',')) for line in lines] print(lst)<\/pre>\n<p>Feel free to check out my detailed video in case you need a refresher on the powerful Python concept list comprehension:<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"A Simple Introduction to List Comprehension in Python\" width=\"780\" height=\"439\" src=\"https:\/\/www.youtube.com\/embed\/9qsq2Vf48W8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Related Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\" target=\"_blank\" rel=\"noreferrer noopener\">Understanding List Comprehension in Python<\/a>.<\/p>\n<div class=\"wp-block-cover aligncenter\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-80 has-background-dim\"><\/span><img decoding=\"async\" loading=\"lazy\" width=\"642\" height=\"642\" class=\"wp-block-cover__image-background wp-image-426802\" alt=\"\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png\" data-object-fit=\"cover\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square.png 642w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-300x300.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/finxter_square-150x150.png 150w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-base-3-color has-text-color has-x-large-font-size\"><strong>Become a One-Liner Wizard!<\/strong><\/p>\n<\/div>\n<\/div>\n<p>In case you enjoyed the <a href=\"https:\/\/blog.finxter.com\/python-one-line-x\/\" data-type=\"post\" data-id=\"10612\" target=\"_blank\" rel=\"noreferrer noopener\">one-liners<\/a> presented here and you want to improve your Python skills, feel free to get yourself a copy of my best-selling Python book:<\/p>\n<h2>Python One-Liners Book: Master the Single Line First!<\/h2>\n<p><strong>Python programmers will improve their computer science skills with these useful one-liners.<\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium is-resized\"><a href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-1024x944.jpg\" alt=\"Python One-Liners\" class=\"wp-image-10007\" width=\"512\" height=\"472\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-300x277.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-768x708.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n<p><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Python One-Liners<\/em> <\/a>will teach you how to read and write &#8220;one-liners&#8221;: <strong><em>concise statements of useful functionality packed into a single line of code. <\/em><\/strong>You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.<\/p>\n<p>The book&#8217;s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. <\/p>\n<p>Detailed explanations of one-liners introduce <strong><em>key computer science concepts <\/em><\/strong>and<strong><em> boost your coding and analytical skills<\/em><\/strong>. You&#8217;ll learn about advanced Python features such as <em><strong>list comprehension<\/strong><\/em>, <strong><em>slicing<\/em><\/strong>, <strong><em>lambda functions<\/em><\/strong>, <strong><em>regular expressions<\/em><\/strong>, <strong><em>map <\/em><\/strong>and <strong><em>reduce <\/em><\/strong>functions, and <strong><em>slice assignments<\/em><\/strong>. <\/p>\n<p>You&#8217;ll also learn how to:<\/p>\n<ul>\n<li>Leverage data structures to <strong>solve real-world problems<\/strong>, like using Boolean indexing to find cities with above-average pollution<\/li>\n<li>Use <strong>NumPy basics<\/strong> such as <em>array<\/em>, <em>shape<\/em>, <em>axis<\/em>, <em>type<\/em>, <em>broadcasting<\/em>, <em>advanced indexing<\/em>, <em>slicing<\/em>, <em>sorting<\/em>, <em>searching<\/em>, <em>aggregating<\/em>, and <em>statistics<\/em><\/li>\n<li>Calculate basic <strong>statistics <\/strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning<\/li>\n<li>Create more <strong>advanced regular expressions<\/strong> using <em>grouping <\/em>and <em>named groups<\/em>, <em>negative lookaheads<\/em>, <em>escaped characters<\/em>, <em>whitespaces, character sets<\/em> (and <em>negative characters sets<\/em>), and <em>greedy\/nongreedy operators<\/em><\/li>\n<li>Understand a wide range of <strong>computer science topics<\/strong>, including <em>anagrams<\/em>, <em>palindromes<\/em>, <em>supersets<\/em>, <em>permutations<\/em>, <em>factorials<\/em>, <em>prime numbers<\/em>, <em>Fibonacci <\/em>numbers, <em>obfuscation<\/em>, <em>searching<\/em>, and <em>algorithmic sorting<\/em><\/li>\n<\/ul>\n<p>By the end of the book, you&#8217;ll know how to <strong><em>write Python at its most refined<\/em><\/strong>, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.<\/p>\n<p><strong><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Get your Python One-Liners on Amazon!!<\/em><\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>4.5\/5 &#8211; (2 votes) Problem Formulation Given a CSV file (e.g., stored in the file with name &#8216;my_file.csv&#8217;). INPUT: file &#8216;my_file.csv&#8217; 9,8,7 6,5,4 3,2,1 Challenge: How to convert the CSV file to a list of tuples, i.e., putting the row values into the inner tuples? OUTPUT: Python list of tuples [(9, 8, 7), (6, 5, [&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-125940","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\/125940","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=125940"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125940\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=125940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=125940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=125940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}