Python Lists Refresher
The list is one of the most widely used data types in Python. A Python List can be easily identified by square brackets [ ]. Lists are used to store the data items where each data item is separated by a comma (,). A Python List can have data items of any data type, be it an integer type or a boolean type.
One of the leading reasons why lists are being widely used is that Lists are mutable. Being mutable means, any data item of a List can be replaced by any other data item. This makes Lists different from Tuples, which are also used for storing data items but are immutable.
a = ['Hello', 1, 4.63, True, "World", 2.0, "False"]
Indexing in a List are of two types:
1. Positive Indexing – Here the indexing starts from 0, moving from left to right.
2. Negative Indexing – In this, the indexing starts from right to left and the rightmost element has an index value of -1.
Taking the above example as reference, Positive and Negative Indexing will look like:
Based on indices of elements, we can also perform Slicing on Lists.
For example, taking our earlier defined List a,
a[2 : 5] gives [4.63, True, "World"] a[ : ] gives ["Hello", 1, 4.63, True, "World", 2.0, "False"] a[-1: -3 : -1] gives ["False", 2.0]
Similarly, can Replace our existing data item with a new value.
For example, our list a:
a = 2 print(a) gives ['Hello', 2, 4.63, True, "World", 2.0, "False"]
Apart from these operations, Python List has many functions to fulfill anyone’s need. Following are the popular yet never getting old Python Programs on List that will help to build the logical mind of a beginner.
Python List Programs Every Beginner Should Know
1. Program to check if the Given List is in Ascending Order or Not
list1 = [1, 2, 3, 5, 4, 8, 7, 9] temp_list = list1[:] list1.sort() if temp_list == list1: print("Given List is in Ascending Order") else: print("Given List is not in Ascending Order") ''' Expected Output: Given List is not in Ascending Order '''
Explanation: Given a list list1 having 8 int values. This list list1 is assigned to another variable temp_list. Since we are using the .sort() function of the list, it will sort our list list1 which means it will arrange our list1 data items in ascending order.
Thus our original list needed to be stored somewhere. Now our temp_list has an earlier version of list list1 while list1 has become the sorted version of list1. Now apply, if the condition on temp_list and l1. If temp_list is equal to l1, this means that our list was already sorted. If this condition is fulfilled, print Given List is in Ascending Order else print Given List is not in Ascending Order.
2. Program to Find Even Numbers From a List
list2 = [2, 3, 7, 5, 10, 17, 12, 4, 1, 13] for i in list2: if i % 2 == 0: print(i) ''' Expected Output: 2 10 12 4 '''
Explanation: Given a list list2 having 10 int values. We need to find even numbers from the given list. Use for loop to iterate over the list. And for every iteration, use the if condition to check if the list data item at a particular iteration is completely divisible by 2, which means i % 2 should be equal to zero. Now, every item fulfilling this condition will get printed.
3. Program to Merge Two Lists
list3 = [1, 2, 4, 6] list4 = [9, 3, 19, 7] list3.extend(list4) print(list3) ''' Expected Output: [1, 2, 4, 6, 9, 3, 19, 7] '''
Explanation: Given two lists list3 and list4 having certain numerical values. Use the .extend() function of the Python List to merge both lists. Thus, Merging these lists will give a single list having items from the first list followed by the second list. Now print the list3, which will have elements from both the lists list3 and list4. If you want the second list list4 items to be printed first followed by list3 data items, use list4.extend(list3), then print the list4.
4. Interchange First and Last Element of a List
list5 = [1, 29, 51, 9, 17, 6, 7, 23] list5, list5[-1] = list5[-1], list5 print(list5) ''' Expected Output: [23, 29, 51, 9, 17, 6, 7, 1] '''
Explanation: Given a list list5 having certain values. As discussed above, Lists are immutable, thus any data item can be replaced with another value. Use indexing to replace the value. Replace the 0th index with -1st index of list5 and -1st index with 0th index of list5 simultaneously. Now print the updated list list5.
5. Program to Subtract a List from Another List
a = [1, 2, 3, 5] b = [1, 2] l1 =  for i in a: if i not in b: l1.append(i) print(l1) ''' Expected Output: [3, 5] '''
Explanation: Given 2 lists a and b having certain values. To subtract the second list from the first list, we can get those list items from the first list a which are not present in the second list b. Create an empty list l1. Use for loop over the list a, and for every iteration, use if condition along with not in to check if i representing the data item of the list a for that current iteration is not present in list b. If i is not present in list b, append i for that iteration to an empty list l1 created at the beginning. In the end, you will have a populated list l1 having those items exclusive to list a.
6. Program to Get Data Items From a List Appearing Odd Number of Times
x = [1,2,3,4,5,1,3,3,4] l1 =  for i in x: if x.count(i) % 2 != 0: if i not in l1: l1.append(i) print(l1) ''' Expected Output: [2, 3, 5] '''
Explanation: Given a list x having certain values. Create an empty list l1. Use for loop over the list x, and during every iteration, use if condition with .count() function of Python Lists to check the number of occurrences of i in list x where i is representing the data item from the list x at every iteration. Use a nested if condition to select distinct data items from list x having odd occurrences. Now, append i to an empty list l1 created at the beginning. In the end, print the list l1.
Thus, Lists can be used in scenarios where you want to store your values temporarily or to store them longer. Lists are one of Python’s built-in datatypes apart from Tuples, Dictionaries, and Sets.
Each of the mentioned Python Programs can be done more efficiently. The Modus Operandi of solving these Python Programs has been done by keeping the beginner’s mind in consideration. For example, one can use List Comprehensions wherever possible to make the program shorter. Also, can use user-defined functions to increase the versatility of code.
Each program is enclosed with an Expected Output which would come if the given inputs are given. Along with the expected output, an Explanation of the program is given to let the beginner understand the program in the best possible way.
Apart from this, any beginner can find more Lists programs on the internet. Internet is abundant with the resources needed to learn anything. Getting perfection in Lists will help one in any domain, whether in Machine Learning or Web Development in Python.
Although Lists is a wide topic and require practice to excel, the mentioned programs will help any beginner to build the logic on how to use Loops and Conditions along with List functions.
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.