1

I have three list with four values in each list, I have to remove duplicate values from these three list

Here are three lists

country_list = ['USA', 'India', 'China', 'India']
city_list = ['New York', 'New Delhi', 'Beijing', 'New Delhi']
event_list = ['First Event', 'Second Event', 'Third Event', 'Second Event']

As its showing in all three lists "India", "new delhi", and Second event" are repeating, means they again are repeating with each other. I want to remove these repeating value, and wants the result like

country_list = ['USA', 'India', 'China']
city_list = ['New York', 'New Delhi', 'Beijing']
event_list = ['First Event', 'Second Event', 'Third Event']

So how can i get this result is there any function for this ?

e4c5
  • 52,766
  • 11
  • 101
  • 134
Pankaj
  • 229
  • 5
  • 17

3 Answers3

3

One simple way is to do the following:

country_list = list(set(country_list))
city_list = list(set(city_list))
event_list = list(set(event_list))

Hope this helps.

A.N
  • 541
  • 2
  • 13
1

Something like

country_list = list(set(country_list))
city_list = list(set(city_list))
event_list = list(set(event_list))

Ought to do it. This is because a set cannot have duplicates by definition. When you convert your list to a set, the duplicates are discarded. If you want the data to be in a form of a list once again you need to convert it back to a list as shown above. In most cases you can use the set exactly as you would use a list.

for example

for item in set(country_list):
    print item

so the conversion back to list may not be needed.

e4c5
  • 52,766
  • 11
  • 101
  • 134
0

Just use set(). Have a look into this: Python Sets

And this:Sets

For your Lists you can do it like this:

>>> city_list = ['New York', 'New Delhi', 'Beijing', 'New Delhi']

>>> set(city_list)

set(['New Delhi', 'New York', 'Beijing'])

>>> list(set(city_list))

['New Delhi', 'New York', 'Beijing']

Shubham Namdeo
  • 1,845
  • 2
  • 24
  • 40