Instead, you could return the actual string, since the string "NIL" is a true value. E.g., with this approach, you'll have: (string-include 'nil 'vanilla) => nilĭid that return nil because "NIL" is in "VANILLA", because it isn't? It's ambiguous and you can't tell. In your question, you used this example: (string-include 'abd 'abbbe) => nilĪssuming that you're returning the symbols nil and ghf, you'll run into an ambiguity if you ever want to check whether a string contains the substring NIL. In the process of solving this problem, we’ve learned many new Python features that every Python pro coder should know without using Google.Return an index or the substring, not a symbol Loop over all x and call list.append(x.lower()). ![]() You’ve learned three main ways to convert a list of strings to a list of lowercase strings: ![]() □ Recommended Tutorial: Python List Append Method Summary Feel free to check out our full guide on list.append() with many interesting factoids such as which is faster: list.append() or list.extend()? It’s not so bad-mainly due to the efficiency of the list.append() method. Here’s a minimal example: def all_lower(my_list): The default but not so idiomatic way to convert a string list to a lowercase string list is to create an empty list and iterate over all elements in the original list, converting each element to a lowercase string using x.lower() and appending this element to the newly-created list. □ Recommended Tutorial: Python Map Function Method 3: Simple For Loop and List Append □ ![]() If you struggle with the map() function, I’d recommend you check out the following video and blog tutorial: Return list(map(lambda x: x.lower(), my_list))Īn even more concise variant of this is to pass the str.lower() method object right as a first argument of the map() function like so: def all_lower(my_list): Here’s an example on the same sample lists: def all_lower(my_list): The result is a map object that you can convert to a list using the list() built-in function. Method 2: Map Function + String Lower □Ī concise Python one-liner to convert a string list to all lowercase strings is to use the map() function with a lambda function as a first argument - converting a string argument x to its lowercase variant using x.lower() - and the list of strings as the second argument. If you want to convert a string list to all uppercase, check out this detailed tutorial on the Finxter blog. ![]() □ Recommended Tutorial: List Comprehension in Python - A Helpfull Illustrated Guide If you struggle with list comprehension, I’d highly urge you to watch the following explainer video and dive into the related tutorial: Here’s this method applied to our example strings: def all_lower(my_list): The result is a new string list, all lowercase. The most Pythonic way to convert a string list my_list to all lowercase strings is to use the str.lower() method inside a list comprehension expression like so. So, let’s get started with my most recommended solution first! □ Method 1: List Comprehension + str.lower() □ There are multiple great ways to accomplish this coding challenge and I strongly believe it’s a great learning tool to dive into each of them. □ Question: How to convert a list of strings to all lowercase in Python?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |