Change language

Python | Get duplicate tuples from a list

|

Method # 1: Using a comprehension list + set () + count ()
The initial approach that can be taken is that we can iterate over each tuple and check its number in the list with count () , if it is greater than one we can add it to the list. To remove multiple padding, we can convert the result to a set using set () .

# Python3 code to demonstrate how it works
# Get duplicate tuples from the list
# Using comprehension list + set () + count ()

 
# initialize the list

test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ) , 

( 3 , 4 ), ( 4 , 5 ), ( 6 , 7 )]

 
# print original list

print ( "The original list:" + str (test_list))

 
# Get duplicate tuples from the list
# Using comprehension list + set () + count ()

res = list ( set ([ele for ele in test_list

if test_list.count (ele)"  1 ]))

 
# print result

print ( "All the duplicates from list are:" + str (res))

Exit:

 The original list: [(3, 4), (4, 5), (3, 4), (3, 4), (4, 5), (6, 7) ] All the duplicates from list are: [(4, 5), (3, 4)] 

Method # 2: Using Counter () + items () + list comprehension
A combination of the above functions can also be used to accomplish this specific task. In this case, we just get the count of each occurrence of the element, using Counter () as a dictionary, and then retrieve all those with a value greater than 1.

# Python3 code to demonstrate how it works
# Get duplicate tuples from the list
# Usage list comprehension + Counter () + items ()

from collections import Counter

 
# initialize the list

test_list = [( 3 , 4 ), ( 4 , 5 ), ( 3 , 4 ), 

( 3 , 4 ), ( 4 , 5 ), ( 6 , 7 )]

 
# print original list

print ( "The original list:" + str (test_list))

  
# Get duplicate tuples from the list
# Using a list comprehension + Counter () + items ()

res = [ele for ele, count in Counter (test_list) .items ()

if count"  1 ]

 
# print result

print ( "All the duplicates from list are:" + str (res))

Output:

 The original list: [(3, 4), (4, 5), (3, 4), (3, 4), (4, 5), (6, 7)] All the duplicates from list are: [(4, 5), (3, 4)] 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically