How do I generate a list with <N> unique elements randomly selected from another (already existing) list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How do I generate a list with <N> unique elements randomly selected from another (already existing) list?

Hello SoloLearners, I just have an idea to create a `list` which contains unique <N> (a user provided integer value) elements randomly selected from an existing `list`, but I'm not sure how to do that efficiently. I thought about using randint() to generate <N> indices, yes it worked, but there exists duplicates. Example: main_list = [ 1, 2, 3, 4, 5 ] N = 3 # need 3 unique elements Now I need to generate (using loop) something like ... [ 1, 4, 3 ] [ 2, 1, 5 ] ... And so on, where each generated `list` has <N> unique elements randomly selected from <main_list> Please rather not to provide me code. Instead point me in direction, or share me a link to learn from. Big Thanks ...

6th Feb 2023, 6:47 PM
Ipang
26 Answers
+ 10
random.choices() Or generate 3 random integers in the index range of the original list? Or shuffle the list and always take the N first elements?
6th Feb 2023, 7:01 PM
Lisa
Lisa - avatar
+ 8
Ipang , a bit late, but i like to share this code with you. https://code.sololearn.com/cMAUc3DuKw35/?ref=app
7th Feb 2023, 8:29 PM
Lothar
Lothar - avatar
+ 8
sarafoster , may be there is a misunderstanding about random.shuffle(): > shuffle() does *NOT* generate any random numbers, but it uses an existing iterable and *rearrange* the index positions of the iterable members.
8th Feb 2023, 11:49 AM
Lothar
Lothar - avatar
+ 4
I know this probably does not fit your requirements, but given that most other solutions have been discussed, I pitch you UUIDs. :> https://docs.python.org/3/library/uuid.html The pros would be that it's trivial to generate them, can be generated randomly (via uuid4) and given its intended use cases, is virtually impossible to obtain duplicates. However, a UUID is 128 bits long, and reducing that in any way would greatly increase probability of collision. https://code.sololearn.com/cVJUaBRX4oig/?ref=app
8th Feb 2023, 1:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Lothar Thanks, that was the change I had in mind. I was about to try it cause now I realize one shuffle() call surely is far less of a work compared to multiple randint() calls in attempt to generate unique indices + calls to pop() to reduce possible duplicates : )
8th Feb 2023, 6:23 AM
Ipang
+ 3
Bob_Li Does list.copy() make up shallow / deep copy? good idea with shuffling a local copy though 👍 That's what I was thinking of, cause I'd prefer to shuffle a copy and leave the main list intact ...
8th Feb 2023, 6:31 AM
Ipang
+ 3
Adeleke Samuel Bright Osei Please provide ONLY relevant answers/comments.
8th Feb 2023, 6:50 AM
Ipang
+ 3
Ipang shallow copy should be enough unless you're working with nested lists. And yes, shuffle mutating the original list can be problematic if you are reusing the list, thus the need for copy.
8th Feb 2023, 6:52 AM
Bob_Li
Bob_Li - avatar
+ 3
Thanks for the info Bob_Li I got it clearly now ...
8th Feb 2023, 6:53 AM
Ipang
+ 3
Mirielle i think one would have to start with a list of unique items in order to have unique shuffled items. and creating a list of unique ordered numbers is easier than creating a list of unique random numbers. Shuffling a unique sorted list is less costly than generating random numbers and checking if it was already used.
8th Feb 2023, 12:19 PM
Bob_Li
Bob_Li - avatar
+ 2
D_Stark Do you mean the 2nd one?
6th Feb 2023, 8:35 PM
Lisa
Lisa - avatar
+ 2
Ipang, I give you... The shuffled list dealer: comes with the following features: 1. works on a copy. does not mutate the original list. 2. guaranteed no duplication, since it generates a sequential slice of the list. If the original items are unique, the items in the slices are also unique. The slice length can be set from n=1 to n <= list length. 3. shuffle is also made as a default option, which can be turned off. To shuffle or not is your choice. https://code.sololearn.com/cD94P5qG36wp/?ref=app
8th Feb 2023, 1:18 AM
Bob_Li
Bob_Li - avatar
+ 2
Hatsy Rei Hehehe you're right, it is not exactly what I was looking for. However, it's an additional info for thread viewers 😁
8th Feb 2023, 6:35 AM
Ipang
+ 2
I made this as an example it works but memory efficiently not gaurenteed. https://code.sololearn.com/cH5AaiKNuFlG/?ref=app
8th Feb 2023, 5:07 PM
D_Stark
D_Stark - avatar
+ 1
in this case you could use, achieve the desired result by using the "random.sample" function from the "random" module in Python. "random.sample" allows you to randomly sample a specified number of unique elements from a list.
6th Feb 2023, 7:24 PM
ArsenicolupinIII
+ 1
Lisa most efficient 👌
6th Feb 2023, 8:29 PM
D_Stark
D_Stark - avatar
+ 1
Lisa the 3rd one 🙃, because it seems he wants a list of numbers which are shuffled without duplicates. I would create a 2d array fill each row 0-10 and then shuffle each row down the column then take n first from each row.
6th Feb 2023, 9:11 PM
D_Stark
D_Stark - avatar
+ 1
Big Thanks for the answers Jay Matthews Lisa ArsenicolupinIII Mirielle and D_Stark 🙏 Y'all probably will be laughing out should you see how I did with the randint(), such a hassle LOL Well, since we haven't got any reference links posted yet so far, guess I might as well be doing it. https://docs.python.org/3/library/random.html https://code.sololearn.com/c1pZbcxC4JZy/?ref=app
7th Feb 2023, 12:34 AM
Ipang
+ 1
Mirielle Sorry I didn't get it. Which test case was that again? did you mean the <N>? You're right about choices(), it does generate duplicates ... Dunno about random(), haven't implemented it yet ...
7th Feb 2023, 7:16 AM
Ipang
+ 1
Mirielle Well observed! but good news is, sample() seems to manage small <N> unlike choices() ... My next idea is to generate some lists each containing <N> unique elements randomly selected from main list. Just stuck on what to do with the last list that contains leftovers only (less than <N> elements) ... Partially done
7th Feb 2023, 7:24 AM
Ipang