Python Beginner Help: Removing Duplicate Words from Lists

  • Context: Python 
  • Thread starter Thread starter Kuma
  • Start date Start date
  • Tags Tags
    Beginner Python
Click For Summary

Discussion Overview

The discussion revolves around a Python programming problem related to removing duplicate words from multiple lists derived from a text file. Participants explore methods to efficiently combine these lists into a new list without duplicates, focusing on looping techniques and list comprehensions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes the problem of needing to append words from multiple lists into a new list while avoiding duplicates.
  • Another participant suggests using two loops: one to iterate over each list and another to check for duplicates in the resultant list.
  • A participant requests clarification on how to set up the loop to ensure it processes all lists, not just the first one.
  • A later reply proposes using list comprehensions to achieve the desired functionality, indicating that this approach is more efficient.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the looping structure needed to process all lists. There is no consensus on the best method, as different approaches are suggested without agreement on a single solution.

Contextual Notes

Some participants may be missing specific details about the implementation of their loops or the structure of their code, which could affect their ability to progress in solving the problem.

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.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
35
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K