Make All Combinations of a Python List with Itertools | Permutations in Python | Anagram in Python
Key Takeaways
This video teaches how to use itertools to create permutations and anagrams in Python
Full Transcript
dear fellow coders welcome to one little coder in this python tutorial i'm going to quickly show you how you can create permutations of a list out of a string and this idea came from this movie that i was recently watching called enola homes one of the themes that they've got in that movie is you know anagrams that jump switching words and letters and then creating new sentences or new words so if you see this name enola which is the name of the title or character so it if you reverse it it becomes alone so i started thinking you know like how how can we do this in python then i came across this um amazing base amazing library that's part of your python like everybody has got it which makes it really really easy for you to make permutations and combinations like especially with the list so i'm going to show you quickly how to do that and we can see a couple of examples first from eater tools input i'm going to start with combinations okay and when you have combinations then you need a list right for now the first list that i'm going to use is name is equal to um a list which says e n o l a and you've got the name ready so now what we have to do is combinations require two arguments one is the actual list the second one is the number of items that you want to output number of elements that you want to deal with so first for that what we're going to do is we're going to say for i in the range of one to name length of name plus one which means like we want it straight from one till whatever the length is we're going to say that combinations of name comma i so this is going to take this list and it's going to take the number length and then it's going to give you a list so to make it simple i'm going to just print this i'm going to just print this as we go along so let me print this i think it won't work because this is an iterator you have to list it and then print and then when i say print or eta tools oh that's that's a bad way to call that library name so when you run this you can actually see that you know uh for every iteration so let me run this only for two iterations so you know what is actually happening so if i run this for two iterations you can see that you know e n o l a so that is that is there so if i run this for um three iterations oops and this for three you would see that it it goes like this it it takes this this this so you see a three letter uh list you see a two little list and you you just basically see this combination right of different um different con different combination of these letters as the name suggests but what if you actually want to do permutation here which is which is what that happens right where you you try to see um what happens when you try to find anagram of enola so that's what we want to do so for that instead of combinations we need actually permutations so i'm going to copy call permutations here and then instead of this i'm going to do it in a slightly different way because what permutations does is you don't need a for loop for that i mean if you have a for loop it's easy to print out but you don't need there is no length argument i'm going to just put the name here and then i'm going to save for um new pum pum in this and i'm going to print the new boom and when i print this this ideally wouldn't print because this is an iterator or let's see i think we are iterating on it right so it should ideally print so when i print this you can see alone on all these different options right now it wouldn't make a lot of sense because you know you can see that it is it's taking all these so let's quickly do a string join which means i have this empty text string and i want to join with everything that we find here so which means instead of seeing it like this you would see it as a word and when i do that when i run this you can actually see what happened okay so i think i added this here so when i run this you can actually see that i've got a loan i've got i've got different options loan loan so you've got a lot of these different options that you can get when you mix these laters um so right now what we are doing is we are taking the same five letters and trying to make this five letters jumble across all these options that's what permutation is but you can do it combination with the replacement combination without replacement just like we saw and this permutation which actually gives us the anagram so now if you use a library like nltk you can actually filter only those words that occur in english dictionary which means basically you can do anagram and then figure out what are those words like you can jumble across all these letters and then figure out what are those words that are present in english dictionary and just filter them out so you've got the perfect anagram tool um so i might probably create a web application for this this this looks like a nice idea but but yeah for now um if if you are interested in doing permutations or combinations in python you should definitely definitely look into eater tools so they've got a lot of these these amazing options for you to do these things these combinations permutations combinations with replacement in an efficient way and and also all right if it's part of your base python which means you don't you don't need any separate dependencies so yeah that that's it you can use it for a world you can use it for a lot of different things that people are really really interested in these days if you have any questions leave me in the comment section otherwise i would see you in the next video stay safe take
Original Description
After watching Enola Homles and seeing their Anagram Wordplay. I wondered what's the efficient way to do this in Python and Thanks to itertools that's part of base python you can create Permutations and Combinations of List quite easily and efficiently.
Code used - https://github.com/amrrs/youtube-r-snippets/blob/master/python_permuation_combination.py
itertools documentation - https://docs.python.org/3/library/itertools.html
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from 1littlecoder · 1littlecoder · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to create your Free Data Science Blog on Github with Fastpages from Fastai
1littlecoder
Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
1littlecoder
Create your first Data Science Web App using R Shiny
1littlecoder
How to create a Reproducible Example in R using reprex
1littlecoder
No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
1littlecoder
Scrape HTML Table using rvest and Process them for insights using tidyverse in R
1littlecoder
Google Teachable Machine Learning Build No Code AI solution
1littlecoder
Create meaningful fake tidy datasets in R using fakir [#rstats Package]
1littlecoder
How to enable using R Programming with Visual Studio VS Code
1littlecoder
Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
1littlecoder
Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
1littlecoder
Intro to Google Colab - How to use Colab
1littlecoder
Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
1littlecoder
Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
1littlecoder
Do you want a career in Data Science - Tamil Webinar
1littlecoder
Android Smartphone Analysis in R [Live Coding Screencast]
1littlecoder
Programmatically create Images, Memes, Watermarks using Python with imgmaker
1littlecoder
Kaggle Walkthrough to get you started with Data Science - Webinar
1littlecoder
Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
1littlecoder
Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
1littlecoder
Excel to R - Pivot + Bar Chart in Excel & R using tidyverse [Live Coding]
1littlecoder
Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
1littlecoder
5 websites to get Free Real-World Datasets for Data Science/ML Projects
1littlecoder
Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
1littlecoder
Correlation-alternative PPS (Predictive Power Score) Python Package Demo
1littlecoder
Automated Website Screenshots in R using {webshot}
1littlecoder
Installing Custom RStudio Theme (Synthwave85)
1littlecoder
Analyse Google Trends Search Data in R using {gtrendsR}
1littlecoder
3 Tips to ask question on Stack Overflow the right way to get answers
1littlecoder
Learn Data Science with R - Mini Projects - Web Scraping Zomato
1littlecoder
Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
1littlecoder
GET Hackernews Front Page Results using REST API in R
1littlecoder
Quickly deploy ML WebApps from Google Colab using ngrok
1littlecoder
Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
1littlecoder
Plotly Interactive Plots as Pandas Plotting Backend df.plot()
1littlecoder
Stack Overflow Developer Survey 2020 Highlights for New Programmers
1littlecoder
Matplotlib Animation Charts in Python using Celluloid
1littlecoder
Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
1littlecoder
Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
1littlecoder
Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
1littlecoder
Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
1littlecoder
Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
1littlecoder
ggplot2 Text Customization with ggtext | Data Visualization in R
1littlecoder
Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
1littlecoder
YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
1littlecoder
Matplotlib Animated Bar Chart Race in Python | Data Visualization
1littlecoder
Simple Python GUI Development using {guietta}
1littlecoder
#8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
1littlecoder
Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
1littlecoder
Github New Feature Profile Summary/Mini-Resume - Profile Views
1littlecoder
Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
1littlecoder
What is OpenAI GPT-3 - Hype, Examples, Worries
1littlecoder
Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
1littlecoder
Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
1littlecoder
GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
1littlecoder
How to use Advanced Google Search to extract Email Ids from Linkedin
1littlecoder
Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
1littlecoder
Flow - R Package to visualize code logic, functions as a Flow Diagram
1littlecoder
Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
1littlecoder
Create a Pencil Sketch Portrait with Python OpenCV
1littlecoder
Related Reads
📰
📰
📰
📰
Fable 5 Costs 5x More Per Task Than Grok 4.5. Here’s Whether That Actually Matters.
Medium · AI
If AI Writes Your Code, Why Learn Python?
Medium · AI
Stop Hallucinating Terraform: Building a Private Internal Developer Portal with RAG and Gemma 2
Medium · AI
The Poor Guy* Guide to Fable-level Vibe Coding for $80/month
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI