All About Python Lists Methods

All About Python Lists Methods

17 mins read1K Views Comment
clickHere
Updated on Oct 13, 2023 14:03 IST

In this guide, you will learn all about lists in Python: accessing lists, changing list elements, looping through a list, and other list operations with examples.

2022_02_Python-Lists-Methods.jpg

Python offers a rich collection of sequence object types. Sequences are basically ordered collections of objects which are indexed by non-negative integers. They maintain a left-to-right order among the elements they contain. These elements are sorted, referred, and fetched by their relative position. Python has both mutable and immutable sequence types. Let’s begin with the Python lists methods.

The Python list object is the most general mutable sequence type provided by the language. Python lists methods are mutable, that is the contents of the list be modified in place. You can change a list in place, add new elements, overwrite existing elements or delete them as well. Also, note that in a list, the same value can occur more than once.

Let us explore Python Lists Methods in detail!

Also Read: Why Learn Python? Reasons and Top Resources to Learn Python

Explore How to Find an Armstrong Number Using Python

Table of Contents

Access List Items

Just like Python strings, you can access a list element by simply specifying the list’s name followed by its index (offset), enclosed in a square bracket. You can use negative offset as well to access the items from the end of the list. The following image shows a sample Python list along with its elements.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 1: Accessing List Items\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'Python' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Shiksha Online' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Python Courses' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List1 \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 0 \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Python \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List1 \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 2 \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Shiksha Online \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List1 \n <span style="color: black">\n [- \n <span style="color: #ff4500">\n 1 \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Python Courses \n
\n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Python lists are zero-indexed, that is, the index of the first element of a list is 0. If you attempt to access an index outside the bounds of the list, Python will raise an IndexError.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example2: Accessing an index outside the bounds\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'Python' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Shiksha Online' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Python Courses' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List1 \n <span style="color: black">\n [- \n <span style="color: #ff4500">\n 5 \n <span style="color: black">\n ]) \n <span style="color: #808080;font-style: italic">\n ## IndexError: list index out of range \n
\n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Must read- String Template Class in Python

Change List Items

Python lists are mutable sequence types, which means that you can change a list in place, add new elements, overwrite existing ones or delete them as well. You can change the elements of the list using their offset (or index). Again, the list offset needs to be a valid one.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example3: Changing List Items\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'Python' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Shiksha Online' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Python Courses' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
List1 \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 1 \n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 3.0 \n
List1 \n <span style="color: black">\n [- \n <span style="color: #ff4500">\n 1 \n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n 'Python Certifications' \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Updated List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

You can change the contents of the list by assigning to either a particular element (offset) or an entire section of list (slicing). The slicing operation replaces an entire section of a given string just in one step.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example4: Changing a section of list\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'Python' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Shiksha Online' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Python Certifications' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
List1 \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 1: \n <span style="color: #ff4500">\n 3 \n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 3.0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Python Online Courses' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Shiksha Online' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Updated List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Both index and slice assignments change the list in place, that is they modify the original list rather than creating a new list object. You can also modify lists using list methods such as insert, pop, and remove.

Add List Items

You can add an element to the list in different ways. If you want to add an item at a particular index, you can do so using the indexing or slicing assignments. Alternatively, you can also use Python list built-in methods. Let us explore each of these with the help of simple examples.

Adding an item using append()

You can add an element at the end of the list using the append() method as shown below.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example5: Adding item to list using append()\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
List1. \n <span style="color: black">\n append \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n 'abc' \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version 1 of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n
List2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'ABCD' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'WXYZ' \n <span style="color: black">\n ] \n
List1. \n <span style="color: black">\n append \n <span style="color: black">\n (List2 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version 2 of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

If you try to append another list, it becomes a single element at the end of the original list. In the above example, we have List2 [‘ABCD’, ‘WXYZ’], which becomes the last element of the list List1.

Adding an item using insert()

If you want to add an element at a particular index, then you can use the insert(index, value) method as shown below.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example6: Adding item to list using insert(index, value)\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
List1. \n <span style="color: black">\n insert \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 0 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version 1 of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n
List2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'ABCD' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'WXYZ' \n <span style="color: black">\n ] \n
List1. \n <span style="color: black">\n insert \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 10 \n <span style="color: #66cc66">\n , List2 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version 2 of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Notice in the second part of the code that I have specified offset as 10, which is beyond the end of the list. In an obvious sense, it should raise an exception, but it doesn’t. The insert method just adds the new element to the end of the list.

Popular Python Course Providers:

Top Python Courses by Udemy Popular Python Courses by Coursera
Top Python Courses by Udacity Popular Python Courses by PluralSight

Remove List Items

Deleting list items using pop()

You can use the pop() method to get an item from the list and remove it at the same time. If the offset is specified it returns and removes the items at that offset. When no argument is specified pop() returns and removes the last element of the list.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example7: Deleting an item using pop()\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Removed element:" \n <span style="color: #66cc66">\n , List1. \n <span style="color: black">\n pop \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 1 \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version 1 of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Next removed element:" \n <span style="color: #66cc66">\n , List1. \n <span style="color: black">\n pop \n <span style="color: black">\n ( \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version 2 of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Deleting list items using remove()

You can remove an item by value using remove() method as shown below. This method deletes the first occurrence of the value that you specify. If the value is not found it raise ValueError

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example8: Deleting an item using remove\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Removed element:" \n <span style="color: #66cc66">\n , List1. \n <span style="color: black">\n remove \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Deleting list items using del

del is a Python statement that you can use to delete an element at a particular offset. You should note that it is not a method.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 9: Deleting an item using keyword del\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n del List1 \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 5: \n <span style="color: #ff4500">\n 7 \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Looping Through A List

Method 1: Using for loop

The most common way to traverse a list is using looping mechanisms, especially for loop. Here’s a simple example.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 10: Looping through a list: for loop \n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n for items \n <span style="color: #ff7700;font-weight:bold">\n in List1: \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (items \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

This works well if you just want to read the elements of the list. Now if you want to change the elements of the list, you need the indices. A very common way to do that is using range and len as shown below:

Method 2: Using for loop, range(), & len()

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 10: Looping through a list: for loop using range & len\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 4 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 5 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 6 \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n ( \n <span style="color: #008000">\n len \n <span style="color: black">\n (List1 \n <span style="color: black">\n ) \n <span style="color: black">\n ): \n
List1 \n <span style="color: black">\n [i \n <span style="color: black">\n ] \n <span style="color: #66cc66">\n = List1 \n <span style="color: black">\n [i \n <span style="color: black">\n ] * List1 \n <span style="color: black">\n [i \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Update version of List:" \n <span style="color: #66cc66">\n , List1 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

What’s happening here is that the for loop traverses through each element and updates it. len function returns the length of the list ‘n’ and the range function returns a list of indices from 0 to n-1

Method 3: Using while loop

Let us try to print each element and its corresponding offset using a while loop.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 10: Looping through a list: while loop\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
i \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 0 \n
\n <span style="color: #ff7700;font-weight:bold">\n while i \n <span style="color: #66cc66">\n < \n <span style="color: #008000">\n len \n <span style="color: black">\n (List1 \n <span style="color: black">\n ): \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (f \n <span style="color: #483d8b">\n '{i}: {List1[i]}' \n <span style="color: black">\n ) \n
i \n <span style="color: #66cc66">\n = i + \n <span style="color: #ff4500">\n 1 \n
\n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Method 4: Using Python enumerate() method

At times you might want to keep track of the index along with the element. In such cases, you can use the enumerate method to iterate any sequence type. The enumerate() method adds a counter to an iterable and returns an enumerate object. The main advantage of this technique is that you can convert enumerated objects to list or tuple using list() & tuple() respectively.

Syntax: enumerate(iterable, start), where iterable is the sequence type that we want to traverse and start is the number from where the enumerate starts counting. If omitted, the default value is 0.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 11: Looping through a list: using enumerate() method\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #66cc66">\n , res \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n enumerate \n <span style="color: black">\n (List1 \n <span style="color: black">\n ): \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (i \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n ":" \n <span style="color: #66cc66">\n ,res \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Method 5: Using List Comprehensions (The most concrete way to iterate)

There is another way to *iterate through a list, which is using List Comprehensions. If you ask me this is the most concrete way of traversing through a list as it offers the shortest syntax. The example below demonstrates how to use the list comprehension for iterating. We will learn about list comprehensions in the next section.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 12: Looping through a list: using list comprehensions \n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ABC' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'abc' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'XYZ' \n <span style="color: black">\n ] \n
\n <span style="color: black">\n [ \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (x \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n for x \n <span style="color: #ff7700;font-weight:bold">\n in List1 \n <span style="color: black">\n ] \n
\n
\n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Notice that out of all the methods that we discussed this offers the shortest amount of code. The best right? So, let’s learn in detail what list comprehensions are all about!

List Comprehensions

List Comprehension: Basic Expression

The quickest way to build and play with a Python list is using list comprehension. At the end of the previous section, we discussed that list comprehension is the most compact way to deal with a list. The simplest form of list comprehension is:

[expression for item in iterable]

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 13: List comprehensions\n \n
\n \n <span style="color: #808080;font-style: italic">\n \n ### Subtracting 1 from each element of list\n \n
numbers_list \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n [number- \n <span style="color: #ff4500">\n 1 \n <span style="color: #ff7700;font-weight:bold">\n for number \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 6 \n <span style="color: black">\n ) \n <span style="color: black">\n ] \n
numbers_list \n
\n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 0 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 4 \n <span style="color: black">\n ] \n
\n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Ain’t that elegant? As you can see they are coded in square brackets and they have an expression, a looping construct. The code is almost entirely English. The above list comprehension basically says, “Give me number-1 for each element of the list”. As simple as that!

Filtering: List Comprehensions with if Clauses

Now, let’s go a step further and explore the optional if condition in the list comprehensions.

[expression for item in iterable if condition]

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 14: List comprehensions\n \n
numbers_list \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [number \n \n <span style="color: #ff7700;font-weight:bold">\n for number \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 20 \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n if number % \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n == \n <span style="color: #ff4500">\n 1 \n <span style="color: black">\n ] \n
numbers_list \n
\n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

This example builds a new list that only has odd numbers between 1 and 20. Try achieving the same thing using a for loop and compare which is much more readable and compact. Here are a few more examples.

List Comprehensions Examples

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 15: List comprehensions more examples\n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n \n print\n \n <span style="color: black">\n \n (\n \n <span style="color: black">\n [s. \n <span style="color: black">\n upper \n <span style="color: black">\n ( \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n for s \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'sam' \n <span style="color: black">\n ] \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: black">\n [s. \n <span style="color: black">\n strip \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n ':' \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n for s \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'list1:' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'list2:' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n ':list3:' \n <span style="color: black">\n ] \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n #Build a list of Unicode codepoints for a given string (print the corresponding string character as well) only if the codepoints > 50 \n
str1 \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n '@&^%$#&' \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: black">\n [f \n <span style="color: #483d8b">\n '{s}: {ord(s)}' \n <span style="color: #ff7700;font-weight:bold">\n for s \n <span style="color: #ff7700;font-weight:bold">\n in str1 \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: black">\n [f \n <span style="color: #483d8b">\n '{s}: {ord(s)}' \n <span style="color: #ff7700;font-weight:bold">\n for s \n <span style="color: #ff7700;font-weight:bold">\n in str1 \n <span style="color: #ff7700;font-weight:bold">\n if \n <span style="color: #008000">\n ord \n <span style="color: black">\n (s \n <span style="color: black">\n ) \n <span style="color: #66cc66">\n > \n <span style="color: #ff4500">\n 50 \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black">\n \n </span style="color: black">\n \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Nested List Comprehensions With Example

Often you might have to use nested loops. But how do you write list comprehension that is equivalent to nested loops? Let’s check that out. Suppose a string is given to you. Write a Python program that will print all the possible combinations of characters of the string and print them.

Using Nested for Loops

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 16: Nested List comprehensions: For loop equivalent\n \n
str1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #483d8b">\n \n "WXYZ"\n \n
res_list \n \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n ( \n <span style="color: #008000">\n len \n <span style="color: black">\n (str1 \n <span style="color: black">\n ) \n <span style="color: black">\n ): \n
\n <span style="color: #ff7700;font-weight:bold">\n for j \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n (i \n <span style="color: #66cc66">\n , \n <span style="color: #008000">\n len \n <span style="color: black">\n (str1 \n <span style="color: black">\n ) \n <span style="color: black">\n ): \n
res_list. \n <span style="color: black">\n append \n <span style="color: black">\n ( \n <span style="color: black">\n (str1 \n <span style="color: black">\n [i \n <span style="color: black">\n ] \n <span style="color: #66cc66">\n , str1 \n <span style="color: black">\n [j \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (res_list \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66">\n \n </span style="color: #483d8b">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Now try to achieve the same thing using list comprehensions. Here you go!

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 17: Nested List comprehensions\n \n
str1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #483d8b">\n \n "WXYZ"\n \n
res_list \n \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: black">\n (str1 \n <span style="color: black">\n [i \n <span style="color: black">\n ] \n <span style="color: #66cc66">\n , str1 \n <span style="color: black">\n [j \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n ( \n <span style="color: #008000">\n len \n <span style="color: black">\n (str1 \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n for j \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n range \n <span style="color: black">\n (i \n <span style="color: #66cc66">\n , \n <span style="color: #008000">\n len \n <span style="color: black">\n (str1 \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (res_list \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66">\n \n </span style="color: #483d8b">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Simple isn’t it. Just make sure that you maintain the order of the loops in the code. Loop over j is dependent on i, it must follow the for loop over i in the list comprehension. If you swap them, you will get an error.

Sorting Python Lists

Sorting using sort() function

You can sort lists in place using the sort() method. The sort() method uses standard comparison tests and by default sorts the items of the list in ascending order. However, you can change the behavior by passing in the keyword argument. If the items of the list are numeric, they are sorted in ascending order by default, and if the items are strings, they are sorted alphabetically.

Must Read: Python Sorted () Function

We will now learn how to do this with the help of an example:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 18: Sorting Lists using sort()\n \n
numbers \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 5 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 6 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 7.5 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 1.6 \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original list:" \n <span style="color: #66cc66">\n , numbers \n <span style="color: black">\n ) \n
numbers. \n <span style="color: black">\n sort \n <span style="color: black">\n ( \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted list:" \n <span style="color: #66cc66">\n , numbers \n <span style="color: black">\n ) \n
\n
strings \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original list:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n
strings. \n <span style="color: black">\n sort \n <span style="color: black">\n ( \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted list:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Read more- Python Sort List

Let us now try to change the default order of sorting. Also, what if we had a combination of uppercase & lowercase characters in the list items? How do they get sorted?

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 18: Change the default sorting order\n \n
numbers \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 3 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 5 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 6 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 7.5 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 1.6 \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original list:" \n <span style="color: #66cc66">\n , numbers \n <span style="color: black">\n ) \n
numbers. \n <span style="color: black">\n sort \n <span style="color: black">\n (reverse \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n True \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted list:" \n <span style="color: #66cc66">\n , numbers \n <span style="color: black">\n ) \n
\n
strings \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original list:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n
strings. \n <span style="color: black">\n sort \n <span style="color: black">\n ( \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted list:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Mixed sorting \n
strings. \n <span style="color: black">\n sort \n <span style="color: black">\n (key \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n str. \n <span style="color: black">\n lower \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted list:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Normalize to lowercase \n
strings. \n <span style="color: black">\n sort \n <span style="color: black">\n (key \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n str. \n <span style="color: black">\n lower \n <span style="color: #66cc66">\n , reverse \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n True \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted list:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Normalize to lowercase & change the default sort order \n
\n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

As you can see the reverse argument allows you to sort elements in descending order rather than the default way. You can also specify a one-argument function (str.lower) that returns the value to be used when sorting.

Sorting using sorted() function

Alternatively, you can use the sorted() function that will return a sorted copy of the list. Unlike sort(), it does not modify the original list.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 19: Sorting using sorted()\n \n
strings \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original list before sorting:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Sorted Copy of List" \n <span style="color: #66cc66">\n , \n <span style="color: #008000">\n sorted \n <span style="color: black">\n (strings \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Original list after sorting:" \n <span style="color: #66cc66">\n , strings \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code
Variables in Python
Variables in Python
In this article, we will discuss variables in python, how to name them and how to work with the variables.
All About Polymorphism in Python
All About Polymorphism in Python
In Python, polymorphism means the same function name is being used for different types. In this article, we will learn about polymorphism and how to use it with inheritance.
File Handling in Python – Open, Read, Write, and Close
File Handling in Python – Open, Read, Write, and Close
File handling in Python is an important part of any web application. Python offers several functions for opening, reading, creating, writing, closing, or deleting files in python. In this article,...read more

Copy Lists

When you use default assignment “=”, it assigns a reference of the original list to the new name. However, the original name and new name both are pointing to the same list, which is not what you want. If you want to create a copy of a list, you can use the following techniques.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 20: Copying List\n \n
actual_str \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
\n
\n <span style="color: #808080;font-style: italic">\n #Method 1: Copying by slicing \n
new_str1 \n <span style="color: #66cc66">\n = actual_str \n <span style="color: black">\n [: \n <span style="color: black">\n ] \n
\n
\n <span style="color: #808080;font-style: italic">\n #Method 2: Copying using built-in function list \n
new_str1 \n <span style="color: #66cc66">\n = \n <span style="color: #008000">\n list \n <span style="color: black">\n (actual_str \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n #Method 3: Copying using generic copy.copy() \n
\n <span style="color: #ff7700;font-weight:bold">\n import \n <span style="color: #dc143c">\n copy \n
new_list \n <span style="color: #66cc66">\n = \n <span style="color: #dc143c">\n copy. \n <span style="color: #dc143c">\n copy \n <span style="color: black">\n (actual_str \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #dc143c"> \n </span style="color: #dc143c"> \n </span style="color: #66cc66"> \n </span style="color: #dc143c"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Join Lists

You can concatenate & join lists using various techniques. Let’s look at a couple of them.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 21: Joining Lists\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sam' \n <span style="color: black">\n ] \n
List2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
\n
\n <span style="color: #808080;font-style: italic">\n #Method 1: Using + operator \n
List3 \n <span style="color: #66cc66">\n = List1 + List2 \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List3 \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n #Method 2: Using append() \n
\n <span style="color: #ff7700;font-weight:bold">\n for x \n <span style="color: #ff7700;font-weight:bold">\n in List2: \n
List1. \n <span style="color: black">\n append \n <span style="color: black">\n (x \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List1 \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n #Method 3: Using extend(), which just adds items to end of list \n
List1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sam' \n <span style="color: black">\n ] \n
List2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (List1. \n <span style="color: black">\n extend \n <span style="color: black">\n (List2 \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Join lists using join()

Apart from these methods, you can use the join() method to join lists. You can think of the join method as a counterpart to the split() method. You can use join to combine a list with another, separated by delimiters such as comma, a hyphen, etc.

Syntax: str_name.join(iterable)

  • Str_name: delimiter that joins the list
  • Iterable: The list with set of elements and joins with a delimiter
 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 22: Joining Lists using join()\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'jam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'sam' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'ham' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'dam' \n <span style="color: black">\n ] \n
str2 \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n ' - ' \n <span style="color: #808080;font-style: italic">\n #delimiter \n
str2 \n <span style="color: #66cc66">\n = str2. \n <span style="color: black">\n join \n <span style="color: black">\n (List1 \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n # print the join list \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Concatenated List1 using join() function and delimiter:" \n <span style="color: #66cc66">\n , str2 \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

In case the delimiter is a space character, the join() puts the space between the items of the list. If you want to concatenate without spaces, you can use the empty string, ‘ ‘, as s delimiter.

List of Lists

As you might already know, a list can have elements of any type of element and that includes other lists. So, you can have a list within a list called a nested list or a two-dimensional list.

Creating list of lists

You can create a list of lists using the append method. You can also make use of list comprehensions.

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Example 23: Creating List of Lists\n \n
List1 \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style="color: black">\n ] \n
List2 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'x' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'y' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'z' \n <span style="color: black">\n ] \n
List3 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'A' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'B' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'C' \n <span style="color: black">\n ] \n
\n
\n <span style="color: #808080;font-style: italic">\n #using append() \n
res_lst \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: black">\n ] \n
res_lst. \n <span style="color: black">\n append \n <span style="color: black">\n (List1 \n <span style="color: black">\n ) \n
res_lst. \n <span style="color: black">\n append \n <span style="color: black">\n (List2 \n <span style="color: black">\n ) \n
res_lst. \n <span style="color: black">\n append \n <span style="color: black">\n (List3 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (res_lst \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n #using list comprehensions (or you can use for loops) \n
l1 \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 1 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 3 \n <span style=