léo
léo

Reputation: 7

How can I delete duplicate numbers in a list (Python)?

I have a list :

words = ['1,2,3,4',
         '5,7,8,4',
         '1,2,3,9']

And my aim is to create a new list from words, but without duplicate numbers.

Here an example of what i would like to have!

new_list=[1,2,3,4,5,7,8,9]

I did it :


words = ['1,2,3,4',
         '5,7,8,4',
         '1,2,3,9']

new_list = []
for i in words :
    if i not in new_list:
        new_list.append(i)
print(new_list)

But I got again same number in my list :

['1,2,3,4', '5,7,8,4', '1,2,3,9']

EDIT :

I would like do same thing but with real words, like it:


[" apple is not good",  "mongo is delicious",  banana is very good"] 

and my new list must be every words of these phrases who are unique. Here an example of my results I want:

["apple,is,not,good,mongo,delicious,banana,very"]

Like you can see, I got only unique words from phrases of the list.

Upvotes: 0

Views: 164

Answers (3)

GaryMBloom
GaryMBloom

Reputation: 5699

words = ['1,2,3,4',
         '5,7,8,4',
         '1,2,3,9']

new_list = []
for w in words :
    for i in map(int, w.split(',')):
        if i not in new_list:
            new_list.append(i)
print(new_list)

The trick is extracting the digits from each word in your list. Each word is a string series of numbers separated by commas. So, w.split(',') splits each string of comma-separated numbers into a list. The map() function applies the int() method to each number string converting it into a numeric value. Then that value is added to the new_list if it is not already there.

This solution also handles numbers greater than 9.

Also, as an example to help understand map:

"1,2,3,4".split(",") --> ["1", "2", "3", "4"]

and

map(int, ["1", "2", "3", "4"]) --> [1, 2, 3, 4]

Upvotes: 0

Seyi Daniel
Seyi Daniel

Reputation: 2379

You can combine the elements of words with itertools chain module, then the set() method to eliminate all duplicates:

import itertools
words = ['1,2,3,4','5,7,8,4','1,2,3,9']
c = list(set(itertools.chain(words[0].replace(',',''),words[1].replace(',',''),words[2].replace(',',''))))
new_words = [int(x) for x in c]
new_words.sort()
print(new_words)

Result

[1, 2, 3, 4, 5, 7, 8, 9]

For the Update

import itertools
words = ["apple is not good",  "mongo is delicious",  "banana is very good"] 
new_words = list(set(itertools.chain(words[0].split(' '), words[1].split(' '), words[2].split(' '))))
print(new_words)

Result

['not', 'mongo', 'is', 'apple', 'good', 'very', 'delicious', 'banana']

Upvotes: 1

RomanR
RomanR

Reputation: 111

You need to use extend,  not append. And also split each line. Convert to "set" to remove duplicate items
words = ['1,2,3,4',
             '5,7,8,4',
             '1,2,3,9']

new_list = []
for i in words :
        new_list.extend(i.split(','))
new_list = list(set(new_list))
new_list.sort()
print(new_list)

or

keep the order of elements

    words=['apple,banana,orange', 'apple, mango']

    new_list = []
    for i in words :
            new_list.extend(i.split(','))
    result =[]
    for i in new_list:
        if i not in result:
            result.append(i)
    
    print(result)

Upvotes: 0

Related Questions