0

I was wondering if there was a way to convert my List into a String.

Example: Is there a way to convert MyList = ['a', 'b', 'c', 'd', 'e'] to MyString = "abcde" ?

Reason: I am trying to make words out of those 5 letters: bag, bad, cab, bed...

I tried creating a Label that shows MyList._contains_("bad") but it doesn't work since "bad" is not in the list as a whole, but 'b', 'a', 'd' are in the list individually.

So I thought if I convert the entire List to one String, I would be able to use something like MyString._contains_("bad") and have the word show in the Label.

So is there a way to convert all this to one string? Or is there a more efficient way to solve this issue?

Thanks

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
user2456977
  • 3,830
  • 14
  • 48
  • 87
  • Note it's two underscores - `__contains__` – jonrsharpe Sep 11 '14 at 21:09
  • Once joined, you can use `bad in mystring` rather than `mystring.__contains__("bad")` – khelwood Sep 11 '14 at 21:11
  • 2
    I voted to re-open on the grounds that the OP asked an xy problem. Their actual problem was "How do I find if all characters in a string are contained in a list of characters?" – Cory Kramer Sep 11 '14 at 21:12
  • Note that your method won't work anyway, since `'bad' in 'abcde'` is `False`. Try instead: `for word in wordlist: all(letter in MyString for letter in word)` – Adam Smith Sep 11 '14 at 21:12
  • 1
    `from collections import Counter;bool(Counter'bed')-Counter('abcdef')`. Note that `bool(Counter'cede')-Counter('abcdef')` would be evaluate to `False` because `'abcdef'` does not have two `'e'`s in it. If you wanted that to be `True` replace `Counter` with `set`. – Steven Rumbalski Sep 11 '14 at 21:16
  • @StevenRumbalski a bit obscure, but far more correct since `all(letter in 'ab' for letter in 'abba')` is `True` when it certainly ought to be `False`. – Adam Smith Sep 11 '14 at 21:19
  • thanks for all the answers. I searched a lot and couldn't find any results on this matter. Not sure why I didn't get better search results. The answer to his question is similar to mine but it was for a very different scenario. Sorry if it was too similar to his question. I'll try to do better research next time. – user2456977 Sep 11 '14 at 21:30

1 Answers1

3
>>> MyList = ['a', 'b', 'c', 'd', 'e']
>>> ''.join(MyList)

'abcde'

The solution to your other question

>>> MyList = ['a', 'b', 'c', 'd', 'e']

>>> all(letter in MyList for letter in 'bad')
True

>>> all(letter in MyList for letter in 'test')
False
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    `from collections import Counter;bool(Counter'bed') - Counter('abcdef')`. Note that `bool(Counter'cede') - Counter('abcdef')` would evaluate to `False` because `'abcdef'` does not have two `'e'`s in it. If you wanted that to be `True` replace `Counter` with `set`. – Steven Rumbalski Sep 11 '14 at 21:21