{"id":112360,"date":"2021-11-23T12:44:00","date_gmt":"2021-11-23T17:44:00","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=112360"},"modified":"2022-11-21T09:49:33","modified_gmt":"2022-11-21T14:49:33","slug":"python-collections-tutorial","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/","title":{"rendered":"Python Collections Tutorial"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this post, we\u2019ll discuss the underrated Python&nbsp;<strong>collections<\/strong>&nbsp;package, which is part of the standard library.&nbsp;<strong>Collections<\/strong>&nbsp;allows you to utilize several data structures beyond base Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-get-a-count-of-all-the-elements-in-a-list\"><strong>How to get a count of all the elements in a list<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One very useful function in&nbsp;<strong>collections<\/strong>&nbsp;is the&nbsp;<em>Counter<\/em>&nbsp;method, which you can use to return a count of all the elements in a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#91;3, 3, 4, 1, 10, 10, 10, 10, 5]\ncollections.Counter(nums)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"439\" height=\"91\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-count-elements-in-a-list-the-automatic-net.png\" alt=\"\" class=\"wp-image-112367 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-count-elements-in-a-list-the-automatic-net.png 439w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-count-elements-in-a-list-the-automatic-net-300x62.png 300w\" data-sizes=\"(max-width: 439px) 100vw, 439px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 439px; aspect-ratio: 439\/91;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<em>Counter<\/em>&nbsp;object that gets returned is also modifiable. Let\u2019s define a variable equal to the result above.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counts = collections.Counter(nums)\n \ncounts&#91;20] += 1<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"516\" height=\"132\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-counter-collections-automaticnet.png\" alt=\"\" class=\"wp-image-112373 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-counter-collections-automaticnet.png 516w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-counter-collections-automaticnet-300x77.png 300w\" data-sizes=\"(max-width: 516px) 100vw, 516px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 516px; aspect-ratio: 516\/132;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Notice how we can add the number 20 to our Counter object without having to initialize it with a 0 value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Counter<\/em>&nbsp;can also be used if you have a list of tuples. This can be handy if you want to count pairs of numbers or other types of objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>temp = &#91;(2, 3), (2, 3), (4, 5), (2, 3), (1, 1), (4, 5)]\n \ncollections.Counter(temp)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to create default values for Python dictionaries<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In a previous post, we discussed&nbsp;<a href=\"https:\/\/theautomatic.net\/2020\/04\/15\/all-about-python-dictionaries\/\">Python dictionaries<\/a>.&nbsp;<em>defaultdict<\/em>&nbsp;provides a modified dictionary structure that lets you assign a default value for any key. This means that if you try referencing a key that you haven\u2019t defined,&nbsp;<em>defaultdict<\/em>&nbsp;will return some default value rather than a \u201cKeyError\u201d. The default value can also be a data structure. For example, suppose you want to create a dictionary where you expect each element to be a set.&nbsp;<em>defaultdict<\/em>&nbsp;can automatically create this for you.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sample_map = collections.defaultdict(set)\n \nsample_map&#91;\"a\"].add(5)\nsample_map&#91;\"a\"].add(10)\nsample_map&#91;\"b\"].add(2)\n \nsample_map<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"545\" height=\"255\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-defaultdict-automaticnet.png\" alt=\"\" class=\"wp-image-112379 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-defaultdict-automaticnet.png 545w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-defaultdict-automaticnet-300x140.png 300w\" data-sizes=\"(max-width: 545px) 100vw, 545px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 545px; aspect-ratio: 545\/255;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you try to reference a non-existent key, Python will return an empty set since that\u2019s our default value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sample_map&#91;\"x\"] # returns empty set<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also, note how we don\u2019t need to initialize sample_map[\u201ca\u201d] to an empty set since&nbsp;<em>defaultdict<\/em>&nbsp;takes care of it for us. This works for other data types as well. For example, the code below will create a default dictionary structure for lists.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list_container = collections.defaultdict(list)\n \nlist_container&#91;\"x\"].append(5)\nlist_container&#91;\"x\"].append(3)\n \nlist_container&#91;\"y\"].append(1)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using&nbsp;<em>defaultdict<\/em>&nbsp;is one way of creating&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Adjacency_list\">adjacency lists<\/a>&nbsp;in Python to represent graphs. It can be useful in situations where you need to build out the adjacency list based off some prior logic (like running&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Depth-first_search\">DFS<\/a>, for example).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to sets and lists,&nbsp;<em>defaultdict<\/em>&nbsp;also supports ints (integers) as a default value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>collections.defaultdict(int)\n\n# create a dictionary where the default value is 5\ndef_five = collections.defaultdict(lambda: 5)\n \ndef_five&#91;\"a\"] = 10\n \n# \"b\" is not a key, but running this line\n# will return the default value of 5\ndef_five&#91;\"b\"]<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"488\" height=\"185\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-dictionary-default-values-automatic-net.png\" alt=\"\" class=\"wp-image-112393 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-dictionary-default-values-automatic-net.png 488w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-dictionary-default-values-automatic-net-300x114.png 300w\" data-sizes=\"(max-width: 488px) 100vw, 488px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 488px; aspect-ratio: 488\/185;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>deque<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<em>deque<\/em>&nbsp;data structure in&nbsp;<strong>collections<\/strong>&nbsp;is a double-ended queue.&nbsp;<em>deque<\/em>&nbsp;is pronounced like \u201cdeck\u201d. A&nbsp;<em>deque<\/em>&nbsp;works similarly to a&nbsp;<a href=\"https:\/\/theautomatic.net\/tutorial-for-python-lists\/\">list<\/a>, but is more optimized for appending or popping off elements from either the front of the queue or the end. For instance, if you have a very large list in Python and you want to append an element to the start of the list, this operation takes O(n) time since each element in the list needs to be shifted over by one. In other words, if your list has 100,000 elements and you append an element to the front of the list, this requires 100,000 shift operations to move each element over by one in the list. This could become computationally expensive, especially if you need to do this within a loop, for example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># create a list with 100,000 elements\nnums = list(range(100000))\n \n# append -10 to the front of the list\n# note that this takes O(n) time, or 100,000 operations since \n# each number if shifted over by one in the list\nnums.insert(0, -10)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Doubled-ended queues, on the other hand, can append elements to the front&nbsp;<em>or<\/em>&nbsp;back in approximately O(1) time. Appending elements to the front of the queue can be done using the&nbsp;<em>appendleft<\/em>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>queue = collections.deque()\n \nqueue.appendleft(5)\nqueue.appendleft(4)\nqueue.appendleft(3)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"299\" height=\"459\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-deque-the-automatic-net-1.png\" alt=\"\" class=\"wp-image-112426 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 299px; aspect-ratio: 299\/459;\"><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, to pop elements off from the left side, we can use the&nbsp;<em>popleft<\/em>&nbsp;method. Again, normally if you were to pop an element off from the left side of of a list, it would take O(n) operations, but with a doubled-ended queue, you can do this in O(1) time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>queue.popleft()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Double-ended queues also comes with operations to append multiple elements at once to either the front or back of the queue. For example, Python lists have an&nbsp;<em>extend<\/em>&nbsp;method that adds a list of other elements to the current list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#91;2, 3, 4]\n \nnums.extend(&#91;5, 6, 7])\n \n# nums = &#91;2, 3, 4, 5, 6, 7]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Double-ended queues have an&nbsp;<em>extendleft<\/em>&nbsp;method and an&nbsp;<em>extend<\/em>&nbsp;method to be able to do this for the left and right sides of the list, respectively.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>queue.extendleft(&#91;1, 2, 3, 4, 5])<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"363\" height=\"130\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-deque-extendleft-automatic-net.png\" alt=\"\" class=\"wp-image-112405 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-deque-extendleft-automatic-net.png 363w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-deque-extendleft-automatic-net-300x107.png 300w\" data-sizes=\"(max-width: 363px) 100vw, 363px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 363px; aspect-ratio: 363\/130;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Named tuples<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Another data structure available is the named tuple. If you\u2019ve used R, you may be familiar with the concept of named vectors where you can reference elements in a vector not only by index, but only by a name. Similarly, named tuples offered by&nbsp;<strong>collections<\/strong>&nbsp;let you reference tuple elements by name or index.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Info = collections.namedtuple(\"info\", &#91;\"a\", \"b\", \"c\"])\ntest = Info(a = 10, b = 20, c = 30)\n \n# reference elements by index\ntest&#91;0] + test&#91;1]\n \n# reference elements by name\ntest.a + test.b<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"589\" height=\"272\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-named-tuple-automatic-net.png\" alt=\"\" class=\"wp-image-112410 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-named-tuple-automatic-net.png 589w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/11\/python-named-tuple-automatic-net-300x139.png 300w\" data-sizes=\"(max-width: 589px) 100vw, 589px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 589px; aspect-ratio: 589\/272;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Named tuples can also be useful when working with sqlite,&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/collections.html#collections.namedtuple\">as seen here.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Visit TheAutomatic.net to learn more about this topic: <a href=\"https:\/\/theautomatic.net\/2021\/04\/14\/python-collections-tutorial\/\">https:\/\/theautomatic.net\/2021\/04\/14\/python-collections-tutorial\/<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.<\/p>\n","protected":false},"author":388,"featured_media":44932,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[339,343,349,338,341,352,344],"tags":[806,595,10671],"contributors-categories":[13695],"class_list":["post-112360","post","type-post","status-publish","format-standard","has-post-thumbnail","category-data-science","category-programing-languages","category-python-development","category-ibkr-quant-news","category-quant-development","category-quant-north-america","category-quant-regions","tag-data-science","tag-python","tag-python-collections","contributors-categories-theautomatic-net"],"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Collections Tutorial | IBKR Quant<\/title>\n<meta name=\"description\" content=\"One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/112360\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Collections Tutorial | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-23T17:44:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:49:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"Python Collections Tutorial\",\n\t            \"datePublished\": \"2021-11-23T17:44:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:49:33+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/\"\n\t            },\n\t            \"wordCount\": 765,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/python-mag-glass.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"Python\",\n\t                \"Python collections\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant North America\",\n\t                \"Quant Regions\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/\",\n\t            \"name\": \"Python Collections Tutorial | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/python-mag-glass.jpg\",\n\t            \"datePublished\": \"2021-11-23T17:44:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:49:33+00:00\",\n\t            \"description\": \"One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-collections-tutorial\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/python-mag-glass.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/python-mag-glass.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Collections Tutorial | IBKR Quant","description":"One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/112360\/","og_locale":"en_US","og_type":"article","og_title":"Python Collections Tutorial | IBKR Quant Blog","og_description":"One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/","og_site_name":"IBKR Campus US","article_published_time":"2021-11-23T17:44:00+00:00","article_modified_time":"2022-11-21T14:49:33+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Andrew Treadway","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"Python Collections Tutorial","datePublished":"2021-11-23T17:44:00+00:00","dateModified":"2022-11-21T14:49:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/"},"wordCount":765,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg","keywords":["Data Science","Python","Python collections"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development","Quant North America","Quant Regions"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/","name":"Python Collections Tutorial | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg","datePublished":"2021-11-23T17:44:00+00:00","dateModified":"2022-11-21T14:49:33+00:00","description":"One very useful function in\u00a0collections\u00a0is the\u00a0Counter\u00a0method, which you can use to return a count of all the elements in a list.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-collections-tutorial\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg","width":900,"height":550,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/python-mag-glass.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/112360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=112360"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/112360\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/44932"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=112360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=112360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=112360"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=112360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}