Python Forum
Trying to get unique words from a set of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get unique words from a set of strings
#3
You should never remove items from the thing you are iterating. Instead build a new list that has the desired characteristics.
string ='red,red,white,white,pink,blue,white,white,blue,blue,white'
wordlist = string.split(',')
print(wordlist)

# Make a list only containing unique words
uniquewords = [word for word in wordlist if wordlist.count(word) == 1]
print('Unique colors', uniquewords)

# Make a list where each word only occurs once
colors = set(wordlist)
print('Colors', colors)
Reply


Messages In This Thread
RE: Trying to get unique words from a set of strings - by deanhystad - Apr-15-2020, 03:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 834 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,831 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,882 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Finding multiple strings between the two same strings Slither 1 2,561 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  searching file for unique words Siylo 2 2,564 Nov-20-2018, 08:28 PM
Last Post: wavic
  Compare all words in input() to all words in file Trianne 1 2,798 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  For loops, strings and printing words with even characters Drone4four 8 5,261 Oct-05-2018, 09:23 AM
Last Post: volcano63
  How to find unique and common words per line from a txt file? sweet_swiss 6 4,346 Aug-11-2018, 01:28 PM
Last Post: Gribouillis
  lists, strings, and byte strings Skaperen 2 4,277 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  replace specific words in strings Sama 3 3,705 Jul-23-2017, 01:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020