{"id":1077,"date":"2023-04-20T22:33:00","date_gmt":"2023-04-20T17:03:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1077"},"modified":"2023-08-15T13:27:14","modified_gmt":"2023-08-15T07:57:14","slug":"insert-vs-append-vs-extend","status":"publish","type":"post","link":"https:\/\/geekpython.in\/insert-vs-append-vs-extend","title":{"rendered":"Difference Between insert(), append() And extend() In Python With Examples"},"content":{"rendered":"\n<p><strong>List<\/strong>&nbsp;is one of Python&#8217;s built-in data structures for storing collections of modifiable and ordered data. Lists can hold a variety of data types, including strings, integers, and even lists.<\/p>\n\n\n\n<p>Lists are mutable, which means they can be created and modified. Python provides some methods for modifying the data within the list.<\/p>\n\n\n\n<p>This article will explain the distinctions between the list&nbsp;<strong><em>insert()<\/em><\/strong>,&nbsp;<strong><em>append()<\/em><\/strong>, and&nbsp;<strong><em>extend()<\/em><\/strong>&nbsp;methods. We&#8217;ll see how they differ and how they&#8217;re used to modify the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-methods-objective\"><a href=\"https:\/\/geekpython.in\/insert-vs-append-vs-extend#heading-methods-objective\"><\/a>Methods Objective<\/h2>\n\n\n\n<p>Every Python developer would have worked with lists and used these methods periodically. List&nbsp;<code>insert()<\/code>,&nbsp;<code>append()<\/code>, and&nbsp;<code>extend()<\/code>&nbsp;are somewhat related because they are used to add elements to a list.<\/p>\n\n\n\n<p>However, these methods are syntactically and programmatically distinct, each method has their own way of adding data to the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-list-insert\"><a href=\"https:\/\/geekpython.in\/insert-vs-append-vs-extend#heading-list-insert\"><\/a>List insert()<\/h2>\n\n\n\n<p>We already know what is the meaning of&nbsp;<strong>insert<\/strong>, it means putting or adding a particular element into the data. This method does the same work as its name meaning.<\/p>\n\n\n\n<p>Python list&nbsp;<code>insert()<\/code>&nbsp;method helps to insert an element to the list at the desired position. This method takes two arguments, the first argument is the&nbsp;<strong>index<\/strong>&nbsp;at which the element is to be inserted and the second argument is the&nbsp;<strong>element<\/strong>&nbsp;that will be inserted.<\/p>\n\n\n\n<p><code>list.insert(index, element)<\/code><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of names\nfriends = ['Sachin', 'Rishu', 'Yashwant']\n# Printing the original list\nprint('Original List:', friends)\n# Inserting an element\nfriends.insert(1, 'Abhishek')\n# Printing the updated list\nprint('New List: ', friends)\n\n----------\nOriginal List: ['Sachin', 'Rishu', 'Yashwant']\nNew List:  ['Sachin', 'Abhishek', 'Rishu', 'Yashwant']<\/pre><\/div>\n\n\n\n<p>Using the&nbsp;<code>insert()<\/code>&nbsp;method, we inserted the string&nbsp;<code>'Abhishek'<\/code>&nbsp;at the&nbsp;<strong>1st index<\/strong>&nbsp;in&nbsp;<code>my_lst<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-list-append\"><a href=\"https:\/\/geekpython.in\/insert-vs-append-vs-extend#heading-list-append\"><\/a>List append()<\/h2>\n\n\n\n<p>Python list&nbsp;<code>append()<\/code>&nbsp;method&nbsp;<strong>adds the element to the end of the list<\/strong>. It takes only one argument which is the element to be appended to the list.<\/p>\n\n\n\n<p><code>list.append(element)<\/code><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of names\nfriends = ['Sachin', 'Rishu', 'Yashwant']\n# Printing the original list\nprint('Original List:', friends)\n# Appending an element\nfriends.append(['Abhishek', 'Yogesh'])\n# Printing the updated list\nprint('New List: ', friends)\n\n----------\nOriginal List: ['Sachin', 'Rishu', 'Yashwant']\nNew List:  ['Sachin', 'Rishu', 'Yashwant', ['Abhishek', 'Yogesh']]<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-list-extend\"><a href=\"https:\/\/geekpython.in\/insert-vs-append-vs-extend#heading-list-extend\"><\/a>List extend()<\/h2>\n\n\n\n<p>We&#8217;ve seen how we can use&nbsp;<code>append()<\/code>&nbsp;to add an element to the end of a list. List&nbsp;<code>extend()<\/code>&nbsp;does the same thing, but instead of adding a single element, it&nbsp;<strong>adds each item from the iterable<\/strong>, resulting in the list being extended.<\/p>\n\n\n\n<p><code>list.extend(iterable)<\/code>&nbsp;or&nbsp;<code>list.extend(['elem1', 'elem2', 'elem3'])<\/code><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of names\nfriends = ['Sachin', 'Rishu', 'Yashwant']\n# Printing the original list\nprint('Original List:', friends)\n# Using extend() to extend the list\nfriends.extend(['Abhishek', 'Yogesh'])\n# Printing the updated list\nprint('New List: ', friends)\n\n----------\nOriginal List: ['Sachin', 'Rishu', 'Yashwant']\nNew List:  ['Sachin', 'Rishu', 'Yashwant', 'Abhishek', 'Yogesh']<\/pre><\/div>\n\n\n\n<p>We passed the list in the expression&nbsp;<code>friends.extend(['Abhishek', 'Yogesh'])<\/code>&nbsp;and as we can see the items became part of the original list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-difference\"><a href=\"https:\/\/geekpython.in\/insert-vs-append-vs-extend#heading-difference\"><\/a>Difference<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><td>insert()<\/td><td>append()<\/td><td>extend()<\/td><\/tr><\/thead><tbody><tr><td>Used to add the element at the desired position in the list<\/td><td>Used to add the element at the end of the list<\/td><td>Used to add the elements from the iterable at the end of the list<\/td><\/tr><tr><td><code>list.insert(index, element)<\/code><\/td><td><code>list.append(element)<\/code><\/td><td><code>list.extend(iterable)<\/code><\/td><\/tr><tr><td>Takes two parameters<\/td><td>Takes a single parameter<\/td><td>Takes a single iterable parameter<\/td><\/tr><tr><td>Can take iterable but adds it as it is<\/td><td>Can take iterable but adds it as it is<\/td><td>Takes iterable and each item is added individually<\/td><\/tr><tr><td>List length increases by 1<\/td><td>List length increases by 1<\/td><td>Length of the list increases by the number of items in the iterable<\/td><\/tr><tr><td>Time complexity is constant i.e., O(1)<\/td><td>Time complexity is linear i.e., O(n)<\/td><td>Has time complexity of O(x), where&nbsp;<strong>x<\/strong>&nbsp;is the length of the iterable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Time complexity refers to the computer time to run an algorithm.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/insert-vs-append-vs-extend#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>We&#8217;ve seen how the list&nbsp;<strong><em>insert()<\/em><\/strong>,&nbsp;<strong><em>append()<\/em><\/strong>, and&nbsp;<strong><em>extend()<\/em><\/strong>&nbsp;methods differ from one another in this article. The goal of these methods is the same in that they add elements to the list, but they differ in how they add the elements to the list.<\/p>\n\n\n\n<p>We&#8217;ve compared these methods using the code examples and here&#8217;s a recap of what we&#8217;ve learned<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>insert()<\/code>&nbsp;&#8211; This method is used to insert the element at the desired position in the list<\/li>\n\n\n\n<li><code>append()<\/code>&nbsp;&#8211; This method is used to add the element to the end of the list.<\/li>\n\n\n\n<li><code>extend()<\/code>&nbsp;&#8211; This method is used to add each item from the iterable to the end of the list.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list\" rel=\"noreferrer noopener\">8 different ways to reverse a Python list<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python\" rel=\"noreferrer noopener\">How Python reverse() and reversed() differ from each other<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/ways-to-remove-whitespaces-from-the-string-in-python-with-examples-beginners-guide\" rel=\"noreferrer noopener\">Different ways to remove whitespaces from the string<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/4-ways-of-string-formatting-in-python-guide\" rel=\"noreferrer noopener\">Different types of string formatting in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit\" rel=\"noreferrer noopener\">Build a Covid-19 EDA and Viz streamlit app in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/understanding-args-and-kwargs-in-python-best-practices-and-guide\" rel=\"noreferrer noopener\">What is *args and **kwargs in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/one-liners-in-python\" rel=\"noreferrer noopener\">Powerful one-liners in Python to boost the code<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>List&nbsp;is one of Python&#8217;s built-in data structures for storing collections of modifiable and ordered data. Lists can hold a variety of data types, including strings, integers, and even lists. Lists are mutable, which means they can be created and modified. Python provides some methods for modifying the data within the list. This article will explain [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1079,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[61,12,31],"class_list":["post-1077","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-comparison","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1077"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1077\/revisions"}],"predecessor-version":[{"id":1294,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1077\/revisions\/1294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1079"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}