In this tutorial, we will learn about how to find prime numbers from a List in Python. List is a very important and most used data structure in Python. It has many built-in functions which makes the usage of List even more easy and developer friendly. We will talk about a very common and always asked question, “Find if a number is prime”. There will be many ways to solve this problem. We will specifically talk about various ways to address this problem using List data structure. So, let us begin the tutorial.
Problem Statement:
You have been given a List , “numList” which has 5 integer values in it as shown below
numList = [88, 156, 2, 4, 7 ]
Your Job is to iterate over each element in the List and and check if a number is prime or not. Prime number is a number which has only 2 factor that is 1 and the number itself. For example, 7 is a prime number since it has only two factors, 1 and 7. 8 is not a prime number since It has 4 factors. Similarly, you can find if the given number is prime or not based on its factor.
1. Find Prime and Non-Prime numbers in the List
Also Read: Mastering Bash Concatenate Strings in Linux: [10 Best Examples]
In this task, you have to write a function which will iterate over a given list and finds out which number is prime and which number is non-prime. It will print the output for each number on the console. In the below example, we have passed the list as function argument. Inside isPrime() function, we have used two for loop.
First for loop will iterate over each element in the list. Second for loop will divide the current number(num) from the list from 2 to the num itself. If the current number is divisible by any number other than itself, then we will print the output that the number is not prime. Save below file and execute to see what output it returns.
Solution
def isPrime(numList): for num in numList: for i in range(2, num): if num%i == 0: print(f"{num} is not Prime") break else: print(f"{num} is Prime") numList = [88, 156, 2, 4, 7 ] isPrime(numList)
88 is not Prime
156 is not Prime
2 is Prime
4 is not Prime
7 is Prime
2. Return all prime numbers from the List
In this task, you have to modify your login in previous code so that the function isPrime() returns a list which will only contain prime numbers from the given list. Here, you have to pass the given list as a function argument. Save the below file and execute to see the output.
solution
def isPrime(numList): primeList = [] for num in numList: for i in range(2, num): if num%i == 0: primeList.append(num) break return primeList numList = [88, 156, 2, 4, 7 ] output = isPrime(numList) print(f"All the prime numbers present in given List are: {output}")
All the prime numbers present in given List are: [88, 156, 4]
3. Return all non-prime numbers from the List
In this task, you have to modify your previous code so that the function isPrime() returns a list like you did in previous example, but this time the returned list will contain only the non-prime numbers. Save he below file and execute to see the output.
solution
def isPrime(numList): primeList = [] for num in numList: for i in range(2, num): if num%i == 0: break else: primeList.append(num) return primeList numList = [88, 156, 2, 4, 7 ] output = isPrime(numList) print(f"All the non-prime numbers present in given List are: {output}")
All the non-prime numbers present in given List are: [2, 7]
4. Return Prime and non Prime numbers from the List.
In this task, you have to modify your previous code so that the function, isPrime() returns all the prime and non-prime numbers using dictionary data structure. Save below file and execute to see what output it returns.
solution
def isPrime(numList): primeDict = {} for num in numList: for i in range(2, num): if num%i == 0: primeDict[num] = "Not a Prime Number" break else: primeDict[num] = "Prime Number" return primeDict numList = [88, 156, 2, 4, 7 ] output = isPrime(numList) print(output)
{88: 'Not a Prime Number', 156: 'Not a Prime Number', 2: 'Prime Number', 4: 'Not a Prime Number', 7: 'Prime Number'}
Summary
If you have written all the logics by yourself, then congratulations you have already learnt quite a bit about list and its usage in different ways. It is very important that whenever we solve any coding problem, we must try to solve it in multiple ways and decide on which solution will be more optimized. It will give your understanding more clarity and enhance your skill to address any coding challenge.