Make All Combinations of a Python List with Itertools | Permutations in Python | Anagram in Python

1littlecoder · Intermediate ·💻 AI-Assisted Coding ·4y ago

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 How to create your Free Data Science Blog on Github with Fastpages from Fastai
How to create your Free Data Science Blog on Github with Fastpages from Fastai
1littlecoder
2 Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
1littlecoder
3 Create your first Data Science Web App using R Shiny
Create your first Data Science Web App using R Shiny
1littlecoder
4 How to create a Reproducible Example in R using reprex
How to create a Reproducible Example in R using reprex
1littlecoder
5 No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
1littlecoder
6 Scrape HTML Table using rvest and Process them for insights using tidyverse in R
Scrape HTML Table using rvest and Process them for insights using tidyverse in R
1littlecoder
7 Google Teachable Machine Learning Build No Code AI solution
Google Teachable Machine Learning Build No Code AI solution
1littlecoder
8 Create meaningful fake tidy datasets in R using fakir [#rstats Package]
Create meaningful fake tidy datasets in R using fakir [#rstats Package]
1littlecoder
9 How to enable using R Programming with Visual Studio VS Code
How to enable using R Programming with Visual Studio VS Code
1littlecoder
10 Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
1littlecoder
11 Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
1littlecoder
12 Intro to Google Colab - How to use Colab
Intro to Google Colab - How to use Colab
1littlecoder
13 Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
1littlecoder
14 Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
1littlecoder
15 Do you want a career in Data Science - Tamil Webinar
Do you want a career in Data Science - Tamil Webinar
1littlecoder
16 Android Smartphone Analysis in R [Live Coding Screencast]
Android Smartphone Analysis in R [Live Coding Screencast]
1littlecoder
17 Programmatically create Images, Memes, Watermarks using Python with imgmaker
Programmatically create Images, Memes, Watermarks using Python with imgmaker
1littlecoder
18 Kaggle Walkthrough to get you started with Data Science - Webinar
Kaggle Walkthrough to get you started with Data Science - Webinar
1littlecoder
19 Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
1littlecoder
20 Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
1littlecoder
21 Excel to R - Pivot + Bar Chart in Excel  & R using tidyverse [Live Coding]
Excel to R - Pivot + Bar Chart in Excel & R using tidyverse [Live Coding]
1littlecoder
22 Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
1littlecoder
23 5 websites to get Free Real-World Datasets for Data Science/ML Projects
5 websites to get Free Real-World Datasets for Data Science/ML Projects
1littlecoder
24 Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
1littlecoder
25 Correlation-alternative PPS (Predictive Power Score) Python Package Demo
Correlation-alternative PPS (Predictive Power Score) Python Package Demo
1littlecoder
26 Automated Website Screenshots in R using {webshot}
Automated Website Screenshots in R using {webshot}
1littlecoder
27 Installing Custom RStudio Theme (Synthwave85)
Installing Custom RStudio Theme (Synthwave85)
1littlecoder
28 Analyse Google Trends Search Data in R using {gtrendsR}
Analyse Google Trends Search Data in R using {gtrendsR}
1littlecoder
29 3 Tips to ask question on Stack Overflow the right way to get answers
3 Tips to ask question on Stack Overflow the right way to get answers
1littlecoder
30 Learn Data Science with R - Mini Projects - Web Scraping Zomato
Learn Data Science with R - Mini Projects - Web Scraping Zomato
1littlecoder
31 Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
1littlecoder
32 GET Hackernews Front Page Results using REST API in R
GET Hackernews Front Page Results using REST API in R
1littlecoder
33 Quickly deploy ML WebApps from Google Colab using ngrok
Quickly deploy ML WebApps from Google Colab using ngrok
1littlecoder
34 Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
1littlecoder
35 Plotly Interactive Plots as Pandas Plotting Backend df.plot()
Plotly Interactive Plots as Pandas Plotting Backend df.plot()
1littlecoder
36 Stack Overflow Developer Survey 2020 Highlights for New Programmers
Stack Overflow Developer Survey 2020 Highlights for New Programmers
1littlecoder
37 Matplotlib Animation Charts in Python using Celluloid
Matplotlib Animation Charts in Python using Celluloid
1littlecoder
38 Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
1littlecoder
39 Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
1littlecoder
40 Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
1littlecoder
41 Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
1littlecoder
42 Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
1littlecoder
43 ggplot2 Text Customization with ggtext | Data Visualization in R
ggplot2 Text Customization with ggtext | Data Visualization in R
1littlecoder
44 Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
1littlecoder
45 YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
1littlecoder
46 Matplotlib Animated Bar Chart Race in Python | Data Visualization
Matplotlib Animated Bar Chart Race in Python | Data Visualization
1littlecoder
47 Simple Python GUI Development using {guietta}
Simple Python GUI Development using {guietta}
1littlecoder
48 #8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
#8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
1littlecoder
49 Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
1littlecoder
50 Github New Feature Profile Summary/Mini-Resume - Profile Views
Github New Feature Profile Summary/Mini-Resume - Profile Views
1littlecoder
51 Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
1littlecoder
52 What is OpenAI GPT-3 - Hype, Examples, Worries
What is OpenAI GPT-3 - Hype, Examples, Worries
1littlecoder
53 Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
1littlecoder
54 Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
1littlecoder
55 GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
1littlecoder
56 How to use Advanced Google Search to extract Email Ids from Linkedin
How to use Advanced Google Search to extract Email Ids from Linkedin
1littlecoder
57 Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
1littlecoder
58 Flow - R Package to visualize code logic, functions as a Flow Diagram
Flow - R Package to visualize code logic, functions as a Flow Diagram
1littlecoder
59 Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
1littlecoder
60 Create a Pencil Sketch Portrait with Python OpenCV
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.
Fable 5 costs 5x more per task than Grok 4.5, but its capabilities may justify the extra cost
Medium · AI
📰
If AI Writes Your Code, Why Learn Python?
Explore the implications of AI-generated code on programming skills and the continued relevance of learning Python
Medium · AI
📰
Stop Hallucinating Terraform: Building a Private Internal Developer Portal with RAG and Gemma 2
Learn to build a secure Internal Developer Portal using Terraform RAG pipeline and Gemma 2 to stop hallucinating AI code
Medium · AI
📰
The Poor Guy* Guide to Fable-level Vibe Coding for $80/month
Learn how to achieve Fable-level vibe coding for a fraction of the cost, using affordable alternatives to expensive subscriptions like Claude.
Medium · Programming
Up next
SEO with Claude Code — The Complete Course
Conor Martin
Watch →