Generally, the map() function is used to map values of one collection to another, typically using a different (or even reduced) format.įor instance, you could map each character to another list of booleans, where each boolean represents if the character is uppercase or not. This results in: MKVNLILFSLLVĬonvert List to String in Python with map()įinally, for a more clean and easy way to pass elements through a function - we can use map(). It then uses the eval() function to evaluate the string as Python code, converting it to an actual list. We've passed in the capitalize_char() function into the list comprehension, returning the capitalized version of each element we've passed in. Convert a string list into list using eval Method Initializes a string inilist representing a list and prints it along with its type. M and f have been flipped to lowercase, which shouldn't have happened. String = "".join(capitalize_char(element) for element in base_list) For instance, let's create a function that capitalizes each element in the 'off chance' an element isn't capitalized as it should be: base_list = ĭef capitalize_char( char): return char.upper() You can substitute this with any other function, including custom ones. If you do have to convert the elements, you can simply use a list comprehension: string = "".join( str(element) for element in base_list)Įach element in the list is now passed through the str() function, which converts them to a string. Converting a String into a List of Characters or Words In 63: convert string to a list of characters Word 'Egypt' List1 list(Word) print (List1).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |