Lists in Python is a broad concept and numerous problems can be framed on the same. Since it’s one of the most widely used Python built-in datatypes.
It is important for beginners to become familiar with the topic to the core. This can be achieved by solving a variety of problems based on Lists. Solving problems widens the logical thinking power not limited to [programming but also in solving life problems.
In the previous part, we look upon six highly popular Lists Programs explained in the best way for beginners, in this part, we will continue our list and look upon more sets of Lists problems. First, let’s revisit the Basics of Python Lists again.
Python List Basics
Lists are one of Python’s built-in datatypes used for storing data items. Lists are also known as collection type in Python. One can identify a Python List 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, ranging from string, int to boolean.
One of the primary 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.
list1 = ['Hello', 1, 4.63, True, "World", 2.0, "False"]
Understanding Indexing and Slicing a List is very necessary as it helps building and implementing logic rapidly and in an impromptu manner. Let’s talk about Indexing first.
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:
Now as we know about Indices with respect to Lists, we can perform Slicing in lists effortlessly.
For example, taking our List named list1 which we defined above,
list1[1 : 3] gives [1, 4.63] list1[ : ] gives ["Hello", 1, 4.63, True, "World", 2.0, "False"] list1[ - 1: - 4 : -1] gives ["False", 2.0, "World"]
Now, let’s see how we can replace a data item with another data item in a list. FOr example, our list list1,
list1 = 2.47 print(list1) gives ['Hello', 1, 4.63, 2.47, "World", 2.0, "False"]
Now, let’s look at the Python Lists Programs for Absolute Beginners, continued from Part 1.
Python List Programs
1. Program to Print Reverse List
a = [4, 3, 2, 76, 32, 1, 23] print(a[ : :-1]) ''' Expected Output: [23, 1, 32, 76, 2, 3, 4] '''
Explanation: Given a list having certain elements in it. To print the reverse of this given list, we can use the concept of slicing, as discussed above.
In the slicing, we will include all elements and give a step size of -1. When we give a negative step size, the list traverse in the opposite direction, from right to left. Thus here, a step size of -1 means it will traverse from right to left with a step size of 1 and will include all the elements up to the left end.
2. Program to Check if List is Empty
b = [1, 65, 23, 'Hello', 3.23] if len(b) == 0: print("Given List is Empty") else: print("List is not empty") ''' Expected Output: List is not empty '''
Explanation: Given a list b with a certain number of elements. We used the condition statement here to differentiate an empty list. If the length of a list is 0, this means that the given list has no elements in it. The length of a collection item can be found using the len() function. Thus, if the length of the list is 0, print that the given list is empty else print the given list is not empty.
3. Program to Truncate the List
# Method 1: c = [True, 42, 9.23, 12, 22] c.clear() print(c) ''' Expected Output:  '''
Explanation: Given a list c with a certain number of elements. In this method, we used the .clear() method of Python Lists. This would truncate the list. Thus, if we print our list again, it will give an empty list.
#Method 2: c = [True, 42, 9.23, 12, 22] c *= 0 print(c) ''' Expected Output:  '''
Explanation: Given a list c with a certain number of elements. In this method, we multiplied the list by 0. Conventionally, when a list is multiplied by a number, it concatenates the list by the given number of times. Since we are multiplying by 0, this means we are concatenating 0 times and eventually would get zero.
#Method 3: c = [True, 42, 9.23, 12, 22] c =  print(c) ''' Expected Output:  '''
Explanation: Given a list c with a certain number of elements. In this method, we assigned an empty list to the same variable c. Thus, printing the variable c would give the value of c with the most recent assignment, i.e. an empty list.
4. Program to Find Length of a List
# Method 1 d = [4, 3.12, False, "Python", 66] print(len(d)) ''' Expected Output: 5 '''
Explanation: Given a list d with a certain number of elements. The length of a collection in Python can be found using the len() function. Thus, printing len(d) will give the number of elements inside the list d.
# Method 2 d = [4, 3.12, False, "Python", 66] count = 0 for i in d: count += 1 print(count) ''' Expected Output: 5 '''
Explanation: Given a list d with a certain number of elements. In this method, we used a for loop with a variable count that would increment every time the variable i iterates over elements of list b. Thus the value of variable count would increment up to the number of elements present inside the list d.
5. Program to Find 2nd Smallest Element of a List
e = [14, 57, 2, 43, 29] e.sort() print(e) ''' Expected Output: 14 '''
Explanation: Given a list e with a certain number of elements. To find the 2nd smallest element from a list, here we sorted the list e using the .sort() method. Thus, in a sorted list, an element at index position 1 is the 2nd smallest element of that list.
6. Program to Get Combination of Pair of Elements
import itertools f = [2, 'Hello', 'World', 4.21] print(list(itertools.permutations(f, 2))) ''' Expected Output: [(2, 'Hello'), (2, 'World'), (2, 4.21), ('Hello', 2), ('Hello', 'World'), ('Hello', 4.21), ('World', 2), ('World', 'Hello'), ('World', 4.21), (4.21, 2), (4.21, 'Hello'), (4.21, 'World')] '''
Explanation: Given a list f with a certain number of elements. Here we used the .permutations() method of the library itertools. The .permutations() takes 2 arguments, first the list f and the second, the number of elements we want in each permutation. This will give permutations of elements in a pair.
Thus, understanding Lists in Python is extremely important if one wants to build a career that completely depends upon the usage of Python. Understanding these basic problems would help the beginner in acing the interviews as well.
But the problems in this part and earlier part are not just limited to beginners only. An intermediate or expert can always come back and take a quick overview of these problems as these are the most extremely used and asked Python Lists programs.
In a few of the problems, I have shown more than one method of solving the problem. I would encourage the readers to find their own way of solving these problems and try on different use cases.
As I said in the earlier part as well, these problems can be solved in more best possible ways, but the main objective of this article is to let beginners understand the concepts in the easiest and prompt way. This would help them building logic and framing their own solutions. Solving these problems would also encourage beginners to create their own problems and working towards them.
About the Author
Connect with me on LinkedIn Here.
Check out my other Articles Here
You can provide your valuable feedback to me on LinkedIn.
Thanks for giving your time!
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.You can also read this article on our Mobile APP