Which is Major new features of the 3.9 series as compared to 3.8

Which Is Major New Features Of The 3.9 Series As Compared To 3.8

Finally, Python 3.9.0rc2 is now available for testing Welcome to our blog for the latest news on python programming. so finally python developer announced that 3.9.0rc2 is now available for testing.  Python 3.9.0rc2 is almost ready to do. This release, 3.9.0rc2, is the final planned preview before the last release of Python 3.9 on 2020-10-04.  … Read more

How to remove duplicates from list in python 3

How to remove duplicates from list in python 3 :

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
How to remove duplicates from list in python 3
 
There are different way to remove duplicates from list :
  1. Method 1 : Using list comprehension method
  2. Method 2 : Using list comprehension + enumerate() method
  3. Method 3 : Using set() method
  4. Method 4 : Naive method
  5. Method 5 : Using collections.OrderedDict.fromkeys() method

Method 1 : Using list comprehension 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.

For example :

# 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.

Method 2 : Using list comprehension + enumerate()

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]

Method 3 : Naive method

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]

Read more