## Relation between class and object in python 3.9.1

class and object in python 3.9.1 A class is a blueprint from which you cancreate the instance (object).object is simply a collection of data (variables)and methods (functions) that act on…

Skip to content
# python trending

##
Relation between class and object in python 3.9.1

##
Attributes in python 3.9 : Private,Protected and Global

##
basic python data types with examples

##
what is dict and list comprehensions in python and how to use them

##
generators in python geeks for geeks

##
pickling and unpickling in python tutorials point

##
product of prime factors of 7429 using python code

##
how to create 2D array in python 3.8

##
How to remove duplicates from list in python 3

# How to remove duplicates from list in python 3 :

**There are different way to remove duplicates from list** :
**Method 1 : Using list comprehension method**

**Method 2 : Using list comprehension + enumerate()**

**Method 3 : Naive method**

##
modulo operator in python 3.9

# modulo operator in python 3.9

**Modulo Operator python for negative number:**

class and object in python 3.9.1 A class is a blueprint from which you cancreate the instance (object).object is simply a collection of data (variables)and methods (functions) that act on…

These three attributes are the access modifiers which limit the access of the variables and functions of a class. Attributes in python 3.9 In the object-oriented languages access modifiers are…

Different common built in data types in python with sequence example : Different common built in data types in python with sequence example • Text Type:- Str(String)1)String is a python…

Key point about Dict comprehension Key point about Dict comprehension : 1) Dict is a Dictionary which is used in python. 2)Dictionary is used to store elements. 3)Dict is an…

What are generators in python Generators are used to create sequence of values such as integers, floats, Booleans, strings,etc. Wecan say it as a function that generates iterator. However, main…

Difference Between Pickling And Unpickling In Python In python, pickle is an inbuilt library which is used for serializing and de-serializing the python object.Pickling is used for converting python object…

Enter number to get product of prime factors Step for Calculate Product of Unique Prime Factors of a Number using python program ● The input is taken from the user.●…

how to create 2D array in python 3.8 Welcome everyone, Today we will learn how to create a two-dimensional array in python so let's start:How do you create a two-dimensional…

Hello friends, In this article, we will see How to remove duplicates items or elements from the list in python so Let’s start :

How to remove duplicates from list in python 3 |

- Method 1 : Using list comprehension method
- Method 2 : Using list comprehension + enumerate() method
- Method 3 : Using set() method
- Method 4 : Naive method
- Method 5 : Using collections.OrderedDict.fromkeys() method

This method has working similar to the Naive method , but small difference is it just a liner shorthand of longer methods done with the help of lists comprehension method.

# Demonstrate Python 3 code

# remove duplicated from list using list comprehension

# first we initializing list

use_list = [4, 3, 7, 6, 3, 5, 7, 1]

print (“The main/original list is : ” + str(use_list))

# then we use using list comprehension method

# to remove from list

rest = []

[rest.append(x) for x in use_list if x not in rest]

# printing list output after removal

print (“The unique list after removing dupli is follow: ” + str(rest))

Output :

The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1]

The unique list after removing dupli is follow : [4, 3, 7, 6, 5, 1]

This time to learn different *python terminologies* in python.

list comprehension coupled with enumerate function can also achieve this task. It basically looks for already occurred elements and skips adding them. It preserves the list ordering.

# removing duplicated from list using list comprehension + enumerate()

# first initializing list

use_list = [4, 3, 7, 6, 3, 5, 7, 1]

print (“The main/original list is : ” + str(use_list))

# using list comprehension + enumerate() method to remove duplicated

# from the list

rest = [i for n, i in enumerate(use_list) if i not in test_list[:n]]

print (“TThe unique list after removing dupli is follow : ” + str(rest))

Output :

The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1]

The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]

In this method (naive method), we simply append the first occurrence of the element in new existing list and ignore all the other occurrences of the list.

# Demonstrate Python program

# remove duplicated element from list using list Naive method

# first we initializing list

use_list = [4, 3, 7, 6, 3, 5, 7, 1]

print (“The main/original list is : ” + str(use_list))

# then we use using list Naive method

rest = []

for i in test_list:

if i not in rest:

rest.append(i)

# printing output after removal

print (“The unique list after removing dupli is follow: ” + str(rest))

Output :

The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1]

The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]

Welcome everyone, Today we will see Modulo Operator python,**Modulo Operator python for negative number,**How does a modulo operator work?,What is the use of modulo operator?,How do you find modulo?,What is the symbol of modulus operator?

so let’s start:

Modulo Operator python |

Modulo operator is show by the percent sign (%).

The syntax for Modulo Operator python is :

numx % numy

output:

For example

11 % 2

output:

1

If any case the divisor result is equal to zero then output show ZeroDivisionError:

11 % 0

output:

ZeroDivisionError: modulo by zero.

The modulo operator also work on floating numbers as arguments:**for example:**

4.8 % 1.2

Copy

0.0

Remember % character represents the interpolation operator.(formatting strings)**Examples**

modulo operator is used for different purpose like One of the most common use is to check whether a number is even or odd.

If a given number divided by 2 and has no remainder, then this number is even number. Otherwise it is odd.**EXAMPLE 01) Check for even or odd number:**

numx = 15

if (numx % 5) == 0:

print(num, “is even no.”)

else:

print(num, “is odd no.”)

output:

15 is even no.

If you run the code above, 15 % 5 leaves a remainder of 0 and the code inside the else statement is executed:

15 is even no.**EXAMPLE 02) Check for prime number:**

def is PrimeNumber(numb):

if numb < 1:

return False

for i in range(2, numb):

if (numb % i) == 0:

return False

else:

return True

In this example first, we have to checking if the number, num is a positive number or negative(it’s required positive number). Next step is checking whether the number is divisible by another number in the range from 2 to number without any reminder. If none of the conditions are satisfy, the result is prime number.

Most complex mathematics task is taking modulo of a negative number, which is done behind the program of Python.

(x+y)mod z = [(x mod z)+(y mod z)]mod z

To apply this math to given statement as –

-3 % 7 = ( -1*7 + 4 ) % 7 = 4

This was done so that the (-1*7)%7 will give the answer as 4

Let’s see more examples for better understanding.**Example #1** :

By using this mathematics, we can see able to perform and understand the negative modulo.

11 % 0

output:

ZeroDivisionError: modulo by zero.

filter_none

edit

play_arrow

brightness_4**# Using negative modulo in python**

res1 = -35% 5

res2 = – 17 % 5

print(result1)

print(result2)

Output :

0

2