Python Beginner Help: Removing Duplicate Words from Lists

  • Context: Python 
  • Thread starter Thread starter Kuma
  • Start date Start date
  • Tags Tags
    Beginner Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
Kuma
Messages
129
Reaction score
0

Homework Statement



I have a simple question with python.

I have a text file that's been converted into a bunch of lists like this.
[blah, blah]
[blah, blah, blah]
[a, a, d]
[g,x,d,s,a]

etc..

now i want to loop over all of these lists and append all the words into a new list, but there can't be any duplicate words allowed at all. so for example, there are 3 a's in the above text, only one is allowed in. Same goes for the blah's.

Any ideas? Thanks.

Homework Equations





The Attempt at a Solution



Tried using a for loop but it only does the first line.
 
Physics news on Phys.org
well, (efficiently) checking if there are duplicates in a list/array is an interesting problem...

there are a few ways to do it but you could:

have two loops:
1 that goes over each list in order to append to the resultant list
1 that goes over the resultant list in order to see if the item to be appended is already needed
 
thanks, but do you know how i can set up the loop so it can loop over EVERY list. my loop only does it for the first list and doesn't progress downwards.
 
Kuma said:
thanks, but do you know how i can set up the loop so it can loop over EVERY list. my loop only does it for the first list and doesn't progress downwards.

What's your code like?

iamalexalright has the right solution:
one list comprehension over all your lists, and a second list comprehension to check if the list you're currently looking at is in the list you're building.