When to Use a List Comprehension in Python

Python is famous for allowing you to write code that’s elegant, easy to write, and almost as easy to read as plain English. One of the language’s most distinctive features is the list comprehension, which you can use to create powerful functionality within a single line of code. However, many developers struggle to fully leverage the more advanced features of a list comprehension in Python. Some programmers even use them too much, which can lead to code that’s less efficient and harder to read. By the end of this tutorial,…

Read More

How to Remove an Element From a Python List by Index?

Want to remove an element at a given position i in a list a? Simply use the del a[i] operation to remove the element at index position i. Here’s the Python code example: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] del a[2] print(a) # [0, 1, 3, 4, 5, 6, 7, 8, 9] del a[0] print(a) # [1, 3, 4, 5, 6, 7, 8, 9] del a[-1] print(a) # [1, 3, 4, 5, 6, 7, 8] del a[2:4] print(a) # [1, 3, 6, 7, 8]…

Read More

How to Shuffle a List of Python Objects?

You’ve got a Python list and you want to randomly reorder all elements? No problem, use the shuffle function in Python’s random library. Here’s some code to show you how to do this: import random a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] random.shuffle(a) print(a) # [6, 8, 5, 7, 2, 3, 9, 1, 4, 0] random.shuffle(a) print(a) # [8, 4, 9, 2, 6, 3, 5, 7, 0, 1] random.shuffle(a) print(a) # [1, 0, 7, 2, 4, 9, 5, 8, 3, 6] random.shuffle(a) print(a) # [4,…

Read More

IslandT: Find the position of the only odd number within a list with Python

In this example, we will write a python function that will return the position of the only odd number within the number list. If there is no odd number within that list then the function will return -1 instead. def odd_one(arr): for number in arr: if number % 2 != 0: return arr.index(number) return -1 The method above will loop through the number list to determine the position of the only odd number within that number list. If no odd number has been found then the method above will return…

Read More

JDBC Driver Maven dependency list

Introduction Ever wanted to connect to a relational database using Java and didn’t know which JDBC Driver Maven dependency to use? If so, this article is surely going to help you from now on. Oracle Since September 2019, the Oracle JDBC Driver is available on Maven Central. For JDK 10 and 11, use the following Maven dependency: For JDK 8, use the ojdbc8 artifact instead: For more details about the proper version to use, check out the following Maven Central link. MySQL The MySQL Driver is available on Maven Central,…

Read More
java 

How to find the 3rd (kth) Node from end or tail in a linked list in Java

Hello guys, the problem to find the 3rd element from the end in a singly linked list or Kth node from the tail is one of the tricky but frequently asked linked list problems in Programming job interviews. I know you can easily solve this problem by moving from tail to head or in the reverse direction but the main challenge here is to solve the problem in just one pass. That means, you can not traverse the linked list again and you cannot traverse backward because it's a singly…

Read More

Master Python List Comprehension in 2 Minutes

This tutorial explains Python List Comprehension, the most economical way (in terms of code) to create a list in Python. List Comprehension is also the fastest method to traverse a list, apply some condition, and return a new list with selected elements. You may find it similar to Python filter() method that filters values based on some condition. However, list comprehension has a much powerful syntax which gives options to add expressions and use if statement. Let’s first answer – Why would you want to use List Comprehension? It is…

Read More

Which is Faster: List Comprehension or Map Function in Python?

Python’s creator Guido van Rossum doesn’t like the map() function because it can be easily replaced with list comprehension which is not only faster but also more readable and shorter. Here’s an example: xs = list(range(10)) list_1 = list(map(lambda x: x*x, xs)) list_2 = [x*x for x in xs] print(list_1 == list_2) # True Both lists list_1 and list_2 contain the same data: a series of squared values [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]. The code shows two ways to achieve the same result. Either you…

Read More