Search code examples
pythonlistdifference

How can I split a list in two unique lists in Python?


Hi I have a list as following: listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] 15 members.

I want to turn it into 3 lists, I used this code it worked but I want unique lists. this give me 3 lists that have mutual members.

import random

listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
print(random.sample(listt,5))
print(random.sample(listt,5))
print(random.sample(listt,5))

Solution

  • Try this:

    from random import shuffle
    
    
    def randomise():
        listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
        shuffle(listt)
        return listt[:5], listt[5:10], listt[10:]
    
    print(randomise())
    

    This will print (for example, since it is random):

    (['i', 'k', 'c', 'b', 'a'], ['d', 'j', 'h', 'n', 'f'], ['e', 'l', 'o', 'g', 'm'])