- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Test for Word construction from character list
When it is required to test for word construction from character list, the ‘all’ operator and the ‘count’ method is used.
Below is a demonstration of the same −
my_list = ['p', 'p', 'y', 't', 'h', 'p', 'p', 'y', 'n', 'y', 'y', 't'] print("The list is :") print(my_list) key = 'pyt' print("The key is :") print(key) my_result = all(key.count(chr) <= my_list.count(chr) for chr in key) print("The result is :") if(my_result == True): print("Word can be constructed. ") else: print("Word can’t be constructed. ")
The list is : ['p', 'p', 'y', 't', 'h', 'p', 'p', 'y', 'n', 'y', 'y', 't'] The result is : Word can be constructed.
A list is defined and is displayed on the console.
The value for a key is defined and displayed on the console.
List iteration is used to access all the characters in the list.
It is then compared with the key to check if the key can be constructed.
The ‘all’ operator is used to ensure that all elements are considered.
This is assigned to a variable.
It is displayed as output on the console.
- Python – Test String in Character List and vice-versa
- Python program to find the character position of Kth word from a list of strings
- Python program for most frequent word in Strings List
- Python program to find word score from list of words
- Python program for removing nth character from a string
- Python – Test for all Even elements in the List for the given Range
- Python Program To Get Minimum Element For String Construction
- Python – Test if elements of list are in Min/Max range from other list
- Python program for removing n-th character from a string?
- Python – Test if list is Palindrome
- Python – Test for desired String Lengths
- Program to find the largest grouping of anagrams from a word list in Python
- Output only the first word from select list value - jQuery?
- Remove tuple from list of tuples if not containing any character in Python
- Python Program to return the Length of the Longest Word from the List of Words