0

I have this list:

mylist = [['TX', 'DALLAS'],
 ['TX', 'DALLAS'],
 ['CA', 'LA'],
 ['CA', 'LA'],
 ['ID', 'BOISE']],

I've been trying to write a loop that makes a new list containing only those that were duplicates.

This is my current code:

for i in mylist:
  if mylist.count(i) > 1:
    mylist.remove(i)

mylist

my output:

[['TX', 'DALLAS'], ['CA', 'LA'], ['ID', 'BOISE']]

So the output would be:

[['TX', 'DALLAS'], ['CA', 'LA']],
Owen
  • 178
  • 2
  • 7
  • 2
    "I've been trying to write a loop" Okay. What code did you write? What happened when you tried using that code? What specific problem did you encounter? What is your *question* about that problem? Please read [ask]. – Karl Knechtel Jun 27 '22 at 23:30
  • Make a list containing all the elements and their counts. Then filter that to just the ones where the count > 1. – Barmar Jun 27 '22 at 23:49

1 Answers1

1
list_of_lists = [['TX', 'DALLAS'],
                 ['TX', 'DALLAS'],
                 ['CA', 'LA'],
                 ['CA', 'LA'],
                 ['ID', 'BOISE']]

duplicates = []

for item in list_of_lists:
    if list_of_lists.count(item) > 1 and item not in duplicates:
        duplicates.append(item)

print(duplicates)
Dan Nagle
  • 4,384
  • 1
  • 16
  • 28