Python Program – Strings with all given List characters

PythonServer Side ProgrammingProgramming

When it is required to find strings with all given list characters, a method can be defined that iterates over the elements and uses the ‘+’ operator to determine the result.

Example

Below is a demonstration of the same

def convert_spec_Vals(my_list):
new_val = ""

for element in my_list:
new_val += element

return new_val

my_list = ['p', 'y', 't', 'h', 'o', 'n', '&', 'c', 'o', 'o', 'l']

print("The list is :")
print(my_list)

print("The result is :")
print(convert_spec_Vals(my_list))

Output

The list is :
['p', 'y', 't', 'h', 'o', 'n', '&', 'c', 'o', 'o', 'l']
The result is :
python&cool

Explanation

• A method named ‘convert_spec_Vals’ is defined that takes a list as a parameter, and returns concatenated value using ‘+’ as output.

• An empty string is defined.

• The elements of the list are iterated and added to empty string.

• This is done using the ‘+’ operator.

• Outside the method, a list of characters is defined and is displayed on the console.

• The method is called by passing this list as a parameter.

• This is displayed on the console.

Published on 14-Sep-2021 11:25:59