Python Python Beginner Help: Removing Duplicate Words from Lists

  • Thread starter Thread starter Kuma
  • Start date Start date
  • Tags Tags
    Beginner Python
AI Thread Summary
The discussion centers around a Python programming challenge involving the consolidation of multiple lists into a single list while eliminating duplicates. The user seeks a method to loop through a series of lists, each containing words, and append unique words to a new list. Initial attempts using a single loop only processed the first list, prompting inquiries about how to iterate through all lists effectively. Suggestions include using two nested loops: one to traverse each list and another to check for duplicates in the resultant list. A recommended solution involves employing list comprehensions to streamline the process, allowing for efficient checking of existing items in the new list while iterating through all provided lists.
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.
 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top