Find Palindromes and Anagrams in a List of Strings using Lambda – My programming school

[ad_1]

This tutorial demonstrates how you can discover palindromes and anagrams in a listing of strings using Python lambda. You’ll see absolutely working coding snippets and ought to be capable to execute with Python 3.

We will use a Lambda expression contained in the filter() perform to seek out all of the palindromes and anagrams in the string listing. In Python, we additionally communicate of lambda as the nameless perform, i.e., a perform which doesn’t have a title.

Python Program – Find Palindromes and Anagrams

Let’s first write a Python program which takes a listing of strings as enter, discover the palindromes, and print the outcome.

Use lambda to seek out palindromes in a listing of strings

We’ll apply the nameless perform (Python lambda) to determine palindromes in the listing of strings. First, let’s perceive what’s a palindrome.

A palindrome is a phrase, digits, or a sequence of phrases that comes the identical while studying backward.

See the total logic in the beneath coding snippet.

"""
Program:
 Python program to seek out palindromes in a listing of strings
"""
def get_palindromes(input_string_list): 
   
   print("*******************")
   print("input_string_list = ", input_string_list)
   print("*******************n")

   # Find the listing of palindromes in the strings    
   out_string_list = listing(filter(lambda x: (x.decrease() == "".be part of(reversed(x.decrease()))), input_string_list)) 

   # Print the listing of palindromes in the strings 
   print("*******************")
   print("out_string_list = ", out_string_list)
   print("*******************n")

def Driver(): 
   input_string_list = ['Python', 'Radar', 'CSharp', 'Madam', 'Programmer', 'Noon', 'Refer', 'Php', 'Go']
   get_palindromes(input_string_list)

if __name__=="__main__": 
   Driver()          # name Driver() perform

The outcome of the above Python code snippet is as follows:

*******************
input_string_list =  ['Python', 'Radar', 'CSharp', 'Madam', 'Programmer', 'Noon', 'Refer', 'Php', 'Go']
*******************

*******************
out_string_list =  ['Radar', 'Madam', 'Noon', 'Refer', 'Php']
*******************

Sometimes, you may also must convert a list to string, so have your self go over it.

Use lambda to seek out anagrams in a listing of strings

We’ll once more apply the nameless perform to determine anagrams in the listing of strings. First, let’s perceive what’s an anagram.

An anagram is a phrase or phrase constituted by ordering the characters of a totally different phrase or phrase.

See the total logic in the beneath coding snippet.

"""
Program:
 Python program to seek out anagrams in a listing of strings
"""
from collections import Counter

def get_anagrams(input_string_list, test_string): 
   
   print("*******************")
   print("input_string_list = ", input_string_list)
   print("*******************n")

   # Find the listing of anagrams in the strings    
   out_string_list = listing(filter(lambda x: (Counter(test_string) == Counter(x)), input_string_list)) 

   # Print the listing of anagrams in the strings 
   print("*******************")
   print("out_string_list = ", out_string_list)
   print("*******************n")

def Driver(): 
   input_string_list = ['Python', 'Program', 'Machine', 'yPtnoh', 'Learning']
   test_string = "ntoyPh"
   get_anagrams(input_string_list, test_string)

if __name__=="__main__": 
   Driver()          # name Driver() perform

The outcome of the above Python code snippet is as follows:

*******************
input_string_list =  ['Python', 'Program', 'Machine', 'yPtnoh', 'Learning']
*******************

*******************
out_string_list =  ['Python', 'yPtnoh']
*******************

To study extra, learn our flagship Python tutorial for freshmen and superior learners.

Python  Recommended Post

[ad_2]

Source hyperlink

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



0 thoughts on “Find Palindromes and Anagrams in a List of Strings using Lambda – My programming school”

Leave a Comment