Here is my explanation:
myList = ['Albasit', 'Nahian', 'Hi', 'Kazi', 'Zuljalal', 'Md.', 'Maslinia']
I want to search word in the list with their length. So that I can get the word which is 4 in len.
Here is my explanation:
myList = ['Albasit', 'Nahian', 'Hi', 'Kazi', 'Zuljalal', 'Md.', 'Maslinia']
I want to search word in the list with their length. So that I can get the word which is 4 in len.
Intuitively, you have two options. Filtering or a simple list comprehension.
myList = ['Albasit', 'Nahian', 'Hi', 'Kazi', 'Zuljalal', 'Md.', 'Maslinia']
list(filter(lambda x: len(x) == 4, myList))
# or
[x for x in myList if len(x) == 4]
You can create a new list filtering only the elements with len(element) == 4
myList = ['Albasit', 'Nahian', 'Hi', 'Kazi', 'Zuljalal', 'Md.', 'Maslinia']
new_list = [x for x in myList if len(x) == 4]
print(new_list)
>>> ['Kazi']
str_list = ["these", "are", "some", "random", "strings"]
for my_str in str_list:
if len(my_str) == 3:
print(my_str)
This will print out every 3 Letter word in the list. You can append these to a new list and then print the new list as well. Up to you.
myList = ['Albasit', 'Nahian', 'Hi', 'Kazi', 'Zuljalal', 'Md.', 'Maslinia']
res = list(filter(lambda x: len(x) == 4, myList))
print(res) # ['Kazi']