TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.7K subscribers
Post #9566 1.32K
Shuffling without repetitions:

import random

# Initial list of candidates or prizes
participants = ["Alexey", "Maria", "Ivan", "Olga", "Dmitry"]

# 1. Selecting 3 unique winners (sample without replacement)
winners = random.sample(participants, k=3)
print(f"Winners: {winners}")
# The result is different each time, but there will be no repetitions within the list of winners!

# 2. Shuffling an entire string (creating an anagram)
word = "python"
shuffled_word = "".join(random.sample(word, len(word)))
print(f"Anagram: {shuffled_word}")

# 3. Important difference: random.choices allows repetitions
print(f"With repetitions: {random.choices(participants, k=3)}")

✨ Honest selection and generation of unique sets

When it's necessary to implement the logic of prize draws, random task distribution, or generating test questions, developers often use random.choice() in a loop. But this approach requires manually ensuring that the same element is not selected twice. The random.sample function takes on this routine.

— Guarantee of uniqueness: The main property of random.sample is "without replacement". The extracted element no longer participates in the next selection cycle, which completely eliminates duplicates in the resulting list.

— Safety of the original: The function does not modify the original list (unlike random.shuffle()), but creates a completely new array with the results. This allows the structure of the original data to remain intact.

— Strict control of size: If you pass a parameter k (the number of elements) that exceeds the length of the original list, Python will not start duplicating elements and will immediately throw an ValueError error. This protects the program logic from incorrect data.

#Python #Random #Coding #NoRepetition #DataScience #UniqueSets

✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

🚀 Level up your AI & Data Science skills with HelloEncyclo — a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
✅ 13 courses live + 40+ coming soon
🎯 One access, lifetime updates
🔑 Use code: PRESALE-BOOK-WAVE-2GFG
👉 https://helloencyclo.com/?ref=HUSSEINSHEIKHO
Telegram AI PYTHON 🌟 You’ve been invited to add the folder “AI PYTHON 🌟”, which includes 15 chats.
  • ❤ 4
More from @pythonre
  1. Oct 9, 20267 Days Certified AI Agents with Python: Autonomous Apps Build autonomous AI agents with Py…
  2. Oct 7, 2026A distinctive bot for subscribing to courses on the most popular educational platforms acr…
  3. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  4. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  5. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
  6. Oct 6, 2026Post #9851
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →