{"id":355,"date":"2014-05-09T23:51:17","date_gmt":"2014-05-09T23:51:17","guid":{"rendered":"https:\/\/jassweb.com\/new22\/solved-iterate-over-a-list-in-python\/"},"modified":"2014-05-09T23:51:17","modified_gmt":"2014-05-09T23:51:17","slug":"solved-iterate-over-a-list-in-python-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/","title":{"rendered":"[Solved] Iterate over a list in python"},"content":{"rendered":"<h2> Introduction <\/h2>\n<p>[ad_1]<\/p>\n<p>Python is a powerful programming language that allows you to iterate over a list in a variety of ways. Iterating over a list is a common task in programming, and Python provides several methods for doing so. In this article, we will discuss how to iterate over a list in Python using various methods such as for loops, while loops, list comprehensions, and more. We will also discuss the differences between these methods and when to use each one. By the end of this article, you will have a better understanding of how to iterate over a list in Python.<\/p>\n<h2> Solution<\/h2>\n<p><\/p>\n<p>#1 Using a for loop<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<br \/>\n    print(item)<\/p>\n<p>#2 Using a while loop<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>i = 0<br \/>\nwhile i < len(my_list):\n    print(my_list[i])\n    i += 1 <\/p>\n<p><\/p>\n<div class=\"entry-content\" itemprop=\"text\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/p>\n<p><\/p>\n<div id=\"answer-26297213\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26297213\" data-parentid=\"26296638\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>A simple way is to simply print each string in the returned <code>whois<\/code>:<\/p>\n<pre><code>host=\"stackoverflow.com\"\nwhois = pythonwhois.net.get_whois_raw(host)\nfor item in whois:\n    print item\n<\/code><\/pre>\n<p>This would output something like this:<\/p>\n<pre><code>Domain Name: STACKOVERFLOW.COM \nRegistrar WHOIS Server: whois.name.com \nRegistrar URL: http:\/\/www.name.com \nUpdated Date: 2014-05-09T17:51:17-06:00 \nCreation Date: 2003-12-26T19:18:07-07:00 \nRegistrar Registration Expiration Date: 2015-12-26T19:18:07-07:00 \nRegistrar: Name.com, Inc. \nRegistrar IANA ID: 625 \nRegistrar Abuse Contact Email: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jassweb.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"432221363026032d222e266d202c2e\">[email\u00a0protected]<\/a> \nRegistrar Abuse Contact Phone: +1.17202492374 \nReseller:  \nDomain Status: clientTransferProhibited \nRegistrant Name: Sysadmin Team \nRegistrant Organization: Stack Exchange, Inc. \nRegistrant Street: 1 Exchange Plaza , Floor 26 \nRegistrant City: New York \nRegistrant State\/Province: NY \nRegistrant Postal Code: 10006 \nRegistrant Country: US \netc.\n<\/code><\/pre>\n<p>Anything more elegant than this will require that you use <code>pythonwhois.get_whois(host)<\/code> and then navigate through the returned dictionary formatting and displaying the fields of interest.<\/p>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p> <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p>\n<\/div>\n<\/div>\n<p>solved Iterate over a list in python <\/p>\n<p><script data-cfasync=\"false\" src=\"https:\/\/jassweb.com\/cdn-cgi\/scripts\/5c5dd728\/cloudflare-static\/email-decode.min.js\"><\/script><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/div>\n<p>[ad_2]<\/p>\n<p>Python is a powerful programming language that can be used to create a wide variety of applications. One of the most common tasks in Python is to iterate over a list. This means that you can loop through each item in the list and perform some action on it. In this article, we will look at how to iterate over a list in Python.<\/p>\n<p>The most basic way to iterate over a list in Python is to use a for loop. This loop will go through each item in the list and perform some action on it. For example, if you have a list of numbers, you can use a for loop to print out each number in the list:<\/p>\n<pre>\nnumbers = [1, 2, 3, 4, 5]\n\nfor num in numbers:\n    print(num)\n<\/pre>\n<p>This will print out each number in the list. You can also use a for loop to perform some action on each item in the list. For example, if you have a list of strings, you can use a for loop to print out each string in the list:<\/p>\n<pre>\nstrings = [\"Hello\", \"World\", \"!\"]\n\nfor string in strings:\n    print(string)\n<\/pre>\n<p>This will print out each string in the list. You can also use a for loop to perform some action on each item in the list. For example, if you have a list of numbers, you can use a for loop to calculate the sum of the numbers in the list:<\/p>\n<pre>\nnumbers = [1, 2, 3, 4, 5]\n\ntotal = 0\nfor num in numbers:\n    total += num\n\nprint(total)\n<\/pre>\n<p>This will print out the sum of the numbers in the list. You can also use a for loop to iterate over a list of dictionaries. For example, if you have a list of dictionaries, you can use a for loop to print out each dictionary in the list:<\/p>\n<pre>\ndictionaries = [{\"name\": \"John\"}, {\"name\": \"Jane\"}]\n\nfor dictionary in dictionaries:\n    print(dictionary)\n<\/pre>\n<p>This will print out each dictionary in the list. As you can see, iterating over a list in Python is a simple and straightforward process. With a few lines of code, you can easily loop through each item in the list and perform some action on it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] Python is a powerful programming language that allows you to iterate over a list in a variety of ways. Iterating over a list is a common task in programming, and Python provides several methods for doing so. In this article, we will discuss how to iterate over a list in Python using various &#8230; <a title=\"[Solved] Iterate over a list in python\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\" aria-label=\"More on [Solved] Iterate over a list in python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[349,499,5572],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-solved","tag-python","tag-python-2-7","tag-whois"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Iterate over a list in python - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Iterate over a list in python - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] Python is a powerful programming language that allows you to iterate over a list in a variety of ways. Iterating over a list is a common task in programming, and Python provides several methods for doing so. In this article, we will discuss how to iterate over a list in Python using various ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2014-05-09T23:51:17+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Iterate over a list in python\",\"datePublished\":\"2014-05-09T23:51:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\"},\"wordCount\":506,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"python\",\"python-2.7\",\"whois\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\",\"name\":\"[Solved] Iterate over a list in python - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2014-05-09T23:51:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Iterate over a list in python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Iterate over a list in python - JassWeb","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:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Iterate over a list in python - JassWeb","og_description":"Introduction [ad_1] Python is a powerful programming language that allows you to iterate over a list in a variety of ways. Iterating over a list is a common task in programming, and Python provides several methods for doing so. In this article, we will discuss how to iterate over a list in Python using various ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/","og_site_name":"JassWeb","article_published_time":"2014-05-09T23:51:17+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Iterate over a list in python","datePublished":"2014-05-09T23:51:17+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/"},"wordCount":506,"commentCount":0,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["python","python-2.7","whois"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/","url":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/","name":"[Solved] Iterate over a list in python - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2014-05-09T23:51:17+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-iterate-over-a-list-in-python-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Iterate over a list in python"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}