1

I'm working with a string and a dictionary in Python, trying to loop through the string in order to create a list of the words which appear both in the string and amongst the keys of the dictionary. What I have currently is:

## dictionary will be called "dict" below
sentence = "is this is even really a sentence"
wordsinboth = []
for w in sentence.split():
    if w in dict:
        wordsinboth += w

Instead of returning a list of words split by whitespace, however, this code returns a list of every character in the sentence. The same thing occurs even when I attempt to create a list of split words before looping, as seen below:

sentence = "is this is even really a sentence"
wordsinboth = []
sent = sentence.split()
for w in sent:
    if w in dict:
        wordsinboth += w

I guess I'm not able to specify "if w in dict" and still split by whitespace? Any suggestions on how to fix this?

needaclue
  • 25
  • 4

3 Answers3

0

Use append instead of +=:

sentence = "is this is even really a sentence"
wordsinboth = []
for w in sentence.split():
    if w in dict:
        wordsinboth.append(w)

The += operator doesn't work as you'd expect:

a = [] 
myString = "hello"
a.append(myString)

print(a) # ['hello']

b = [] 
b += myString

print(b) # ['h', 'e', 'l', 'l', 'o']

If you're interested on why this happens, the following questions are a good read:


Also, note that using list comprehensions might result in a more elegant solution to your problem:

wordsinboth = [word for word in sentence.split() if word in dict]
Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
0

You can use += on a list, but you must add a list to it, not a value, otherwise the value gets converted to a list before being added. In your case, the w strings are being converted to a list of all the characters in them (e.g. 'if' => ['i', 'f']). To work around that, make the value into a list by adding [] around it:

for w in sentence.split():
    if w in dict:
        wordsinboth += [w]
Nick
  • 138,499
  • 22
  • 57
  • 95
0

Use list comprehensions it's more shortest and elegant way for your case:

wordsinboth = [word for word in sentence.split() if w in dict]

Problem in your cycle that you have to use append for adding new item to wordsinboth instead of + operator, also please keep in mind that it can create duplicates, if you need uniq items you can wrap your result to set which gives you uniq words.

Like this:

wordsinboth = {word for word in sentence.split() if w in dict}
alex2007v
  • 1,230
  • 8
  • 12