0

I am trying to shuffle a list in python so I could join it with an empty string. Can anyone please explain to me why it doesn't work?

    p = input()
    letters = [x for x in p]
    letters = random.shuffle(letters)
    p = "".join(letters)
Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
  • `random.shuffle()` manipulates the list in place. It doesn't return anything, so you are are assigning `None` back to `letters`. – Mark Oct 28 '21 at 04:09
  • `random.shuffle` shuffles the list in-place and return None rather than return the list shuffled. – enzo Oct 28 '21 at 04:10
  • @Mark so what should i do in order to make it work? – Zakaria Ayadi Oct 28 '21 at 04:11
  • @ZakariaAyadi just call `random.shuffle(letters)` without assigning it back to `letters` – Mark Oct 28 '21 at 04:13
  • In the future, if a method doesn't seem to work the way you expect it to, start by referring to the documentation. [You are expected to research the problem before asking](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). It is as easy as putting `python random.shuffle` [into a search engine](https://duckduckgo.com/?q=python+random.shuffle), which will give you [the official documentation](https://docs.python.org/3/library/random.html#random.shuffle) along with many other useful links (such as the linked duplicate). – Karl Knechtel Oct 28 '21 at 04:37

0 Answers0