{"id":29254,"date":"2021-02-15T16:16:49","date_gmt":"2021-02-16T00:16:49","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=29254"},"modified":"2021-02-22T02:28:04","modified_gmt":"2021-02-22T10:28:04","slug":"how-to-use-python-extend","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/","title":{"rendered":"How to Use Python extend()"},"content":{"rendered":"\n<p>Let\u2019s say that you are going to have a party, and you have a list of friends that you are going to invite. This list may grow as you think of people you want to invite.&nbsp;<br><\/p>\n\n\n\n<p>This is where the Python <em>extend()<\/em> method comes in. The<em> extend()<\/em> method allows you to add an element, or multiple elements, to the end of a list.&nbsp;<br><\/p>\n\n\n\n<p>This article will teach you how to use the Python<em> extend()<\/em> method. We\u2019ll use the invitation list example to illustrate our explanation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Python extend()?<\/h2>\n\n\n\n<p>The Python <em>extend()<\/em> method is one of Python\u2019s list methods. The other list methods are <em>Python append()<\/em> and<em> Python insert()<\/em>. All of these methods allow you to manipulate lists in Python. Let\u2019s dig into why you might want to use the Python <em>.extend()<\/em> method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Python .extend() works<\/h2>\n\n\n\n<p>The <em>.extend()<\/em> method extends a Python list by adding multiple items to the end of the list.&nbsp; Here are two ways you can format the syntax.&nbsp;<br><\/p>\n\n\n\n<p>One way is to put the items you want added to the list (called \u201carguments\u201d) within brackets inside the parentheses. Look at the second line in this example, where we have the new names as arguments within brackets inside parentheses:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>invite_list = ['Shazia', 'Matt', 'Elizabeth']\ninvite_list.extend(['Janie','Bryce','Jaspar'])\nprint(invite_list)<\/pre><\/div>\n\n\n\n<p>This code returns:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>['Shazia', 'Matt', 'Elizabeth', 'Janie', 'Bryce', 'Jaspar']<\/pre><\/div>\n\n\n\n<p>Another way is to declare a new variable called <em>new_names<\/em> and then put this as the argument in the parentheses, as in the third line in this example:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>invite_list = ['Shazia', 'Matt', 'Elizabeth']\nnew_names = ['Janie', 'Bryce', 'Jaspar']\ninvite_list.extend(new_names)\nprint(invite_list)<\/pre><\/div>\n\n\n\n<p>This code returns the same result:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>['Shazia', 'Matt', 'Elizabeth', 'Janie', 'Bryce', 'Jaspar']<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">When Not to Use Python extend()<\/h2>\n\n\n\n<p>If you only want to add one item to the end of your Python list, you can use the <a href=\"https:\/\/careerkarma.com\/blog\/python-append-to-list\/\">Python<em> append() <\/em>method<\/a> instead of the <em>extend()<\/em> method. The Python <em>append()<\/em> method only adds a single item (such as an individual list item or another list) at a time.&nbsp;<br><\/p>\n\n\n\n<p>For example, this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>invite_list = ['Shazia', 'Matt', 'Elizabeth']\nnew_names = ['Janie', 'Bryce', 'Jaspar']\ninvite_list.append(new_names)\nprint(invite_list)<\/pre><\/div>\n\n\n\n<p>results in this output:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>['Shazia', 'Matt', 'Elizabeth', ['Janie', 'Bryce', 'Jaspar']]<\/pre><\/div>\n\n\n\n<p>Notice that the names that were added (Janie, Bryce, and Jaspar) are enclosed in their own set of brackets. This means that the Python <em>append()<\/em> method treated them as a single entity.&nbsp;<br><\/p>\n\n\n\n<p>If you want to insert a list item in a specific position, rather than adding it to the end of the list, you should use the <a href=\"https:\/\/careerkarma.com\/blog\/python-insert\/\">Python <em>insert()<\/em><\/a> method. In Python, list items are assigned positions. These positions are numbers, starting at zero. Therefore, the items in the list <em>[\u2018Shazia\u2019, \u2018Matt\u2019, \u2018Elizabeth\u2019]<\/em> would be assigned 0, 1, 2, 3. \u2018Shazia\u2019 is at position 0, \u2018Matt\u2019 is at position 1, and so on.&nbsp;<br><\/p>\n\n\n\n<p>To use the Python <em>insert()<\/em> method with our invitation list example, we can state the desired position, and the name we want to insert into that position, in the parentheses on the second line of this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>invite_list = ['Shazia', 'Matt', 'Elizabeth']\ninvite_list.insert(0, 'Bryce')\nprint(invite_list)<\/pre><\/div>\n\n\n\n<p>This code returns this result:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>['Bryce', 'Shazia', 'Matt', 'Elizabeth']<\/pre><\/div>\n\n\n\n<p>We have inserted the name \u2018Bryce\u2019 into the 0 position in our list.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Python<em> extend()<\/em> method is used to add items to the end of a list. To do this, you can simply put the new list items in the parentheses when you call the method. Alternatively, you can declare a new variable containing the new list items and then put the variable in the parentheses. The Python <em>extend()<\/em> method differs from the<em> append()<\/em> method, which only adds a single item at a time, and the <em>insert()<\/em> method, which puts a list item in a specific position in the list.&nbsp;<br><\/p>\n\n\n\n<p>Now that you know how to use the Python<em> extend()<\/em> method, you can practice using it, along with the other Python list methods, the next time you code.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"Let\u2019s say that you are going to have a party, and you have a list of friends that you are going to invite. This list may grow as you think of people you want to invite.&nbsp; This is where the Python extend() method comes in. The extend() method allows you to add an element, or&hellip;","protected":false},"author":112,"featured_media":29255,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-29254","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Use Python extend() | Career Karma<\/title>\n<meta name=\"description\" content=\"With Python&#039;s extend() method, you can add items to the end of 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:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Python extend()\" \/>\n<meta property=\"og:description\" content=\"With Python&#039;s extend() method, you can add items to the end of a list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-16T00:16:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-22T10:28:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Karen Schwarze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Karen Schwarze\" \/>\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:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\"},\"author\":{\"name\":\"Karen Schwarze\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ef4d43031318a0eeb598a44661f304cc\"},\"headline\":\"How to Use Python extend()\",\"datePublished\":\"2021-02-16T00:16:49+00:00\",\"dateModified\":\"2021-02-22T10:28:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\"},\"wordCount\":591,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\",\"name\":\"How to Use Python extend() | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg\",\"datePublished\":\"2021-02-16T00:16:49+00:00\",\"dateModified\":\"2021-02-22T10:28:04+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ef4d43031318a0eeb598a44661f304cc\"},\"description\":\"With Python's extend() method, you can add items to the end of a list.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Use Python extend()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ef4d43031318a0eeb598a44661f304cc\",\"name\":\"Karen Schwarze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/Karen_Schwarze-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/Karen_Schwarze-150x150.jpg\",\"caption\":\"Karen Schwarze\"},\"description\":\"A former educator and edtech marketer, Karen is passionate about computer science education. She\u2019s particularly fascinated by the connections between coding languages and English language\/literature structures. This led her to build an edtech app where users create Python programs by manipulating literary devices like similes and metaphors. When she\u2019s not writing, Karen enjoys watching comedy and reading books about history.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/karen-schwarze\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Use Python extend() | Career Karma","description":"With Python's extend() method, you can add items to the end of 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:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Python extend()","og_description":"With Python's extend() method, you can add items to the end of a list.","og_url":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2021-02-16T00:16:49+00:00","article_modified_time":"2021-02-22T10:28:04+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg","type":"image\/jpeg"}],"author":"Karen Schwarze","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Karen Schwarze","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/"},"author":{"name":"Karen Schwarze","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ef4d43031318a0eeb598a44661f304cc"},"headline":"How to Use Python extend()","datePublished":"2021-02-16T00:16:49+00:00","dateModified":"2021-02-22T10:28:04+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/"},"wordCount":591,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/","url":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/","name":"How to Use Python extend() | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg","datePublished":"2021-02-16T00:16:49+00:00","dateModified":"2021-02-22T10:28:04+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ef4d43031318a0eeb598a44661f304cc"},"description":"With Python's extend() method, you can add items to the end of a list.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/02\/wes-hicks-r0DusB-OgRM-unsplash.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/how-to-use-python-extend\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"How to Use Python extend()"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ef4d43031318a0eeb598a44661f304cc","name":"Karen Schwarze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/Karen_Schwarze-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/Karen_Schwarze-150x150.jpg","caption":"Karen Schwarze"},"description":"A former educator and edtech marketer, Karen is passionate about computer science education. She\u2019s particularly fascinated by the connections between coding languages and English language\/literature structures. This led her to build an edtech app where users create Python programs by manipulating literary devices like similes and metaphors. When she\u2019s not writing, Karen enjoys watching comedy and reading books about history.","url":"https:\/\/careerkarma.com\/blog\/author\/karen-schwarze\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/29254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=29254"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/29254\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/29255"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=29254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=29254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=29254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}