I have a Python list variable that contains strings. Is there a function that can convert all the strings in one pass to lowercase and vice versa, uppercase? asked Nov 26, 2009 at 5:15 1 It can be done with list
comprehensions >>> [x.lower() for x in ["A", "B", "C"]]
['a', 'b', 'c']
>>> [x.upper() for x in ["a", "b", "c"]]
['A', 'B', 'C']
or with the map function >>> list(map(lambda x: x.lower(), ["A", "B", "C"]))
['a', 'b', 'c']
>>> list(map(lambda x: x.upper(), ["a", "b", "c"]))
['A', 'B', 'C']
answered Nov 26, 2009
at 5:19 YOUYOU 116k32 gold badges184 silver
badges216 bronze badges 4 Besides being easier to read (for many people), list comprehensions win the speed race,
too: $ python2.6 -m timeit '[x.lower() for x in ["A","B","C"]]'
1000000 loops, best of 3: 1.03 usec per loop
$ python2.6 -m timeit '[x.upper() for x in ["a","b","c"]]'
1000000 loops, best of 3: 1.04 usec per loop
$ python2.6 -m timeit 'map(str.lower,["A","B","C"])'
1000000 loops, best of 3: 1.44 usec per loop
$ python2.6 -m timeit 'map(str.upper,["a","b","c"])'
1000000 loops, best of 3: 1.44 usec per loop
$ python2.6 -m timeit 'map(lambda x:x.lower(),["A","B","C"])'
1000000 loops, best of 3: 1.87 usec per loop
$ python2.6 -m timeit 'map(lambda x:x.upper(),["a","b","c"])'
1000000 loops, best of 3: 1.87 usec per loop
answered Nov 26, 2009 at 5:54 Ned
DeilyNed Deily 81.6k16 gold badges126 silver badges150 bronze badges 5 >>> list(map(str.lower,["A","B","C"]))
['a', 'b', 'c']
Amit JS 1331 silver
badge7 bronze badges answered Nov 26, 2009 at 5:24 ghostdog74ghostdog74 313k55 gold badges252 silver badges339 bronze badges 2 List comprehension is how I'd do it, it's the "Pythonic" way. The following transcript shows how to convert a list to all upper case then back to lower: pax@paxbox7:~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = ["one", "two", "three"] ; x
['one', 'two', 'three']
>>> x = [element.upper() for element in x] ; x
['ONE', 'TWO', 'THREE']
>>> x = [element.lower() for element in x] ; x
['one', 'two', 'three']
answered Nov 26, 2009 at 5:23 paxdiablopaxdiablo 825k227 gold badges1544 silver badges1914 bronze badges 5 For this sample the comprehension is fastest $ python -m timeit -s 's=["one","two","three"]*1000' '[x.upper for x in s]'
1000 loops, best of 3: 809 usec per loop
$ python -m timeit -s 's=["one","two","three"]*1000' 'map(str.upper,s)'
1000 loops, best of 3: 1.12 msec per loop
$ python -m timeit -s 's=["one","two","three"]*1000' 'map(lambda x:x.upper(),s)'
1000 loops, best of 3: 1.77 msec per loop
answered Nov 26, 2009 at 5:59
John La RooyJohn La Rooy 285k50 gold
badges357 silver badges498 bronze badges a student asking, another student with the same problem answering :)) fruits=['orange', 'grape', 'kiwi', 'apple', 'mango', 'fig', 'lemon']
newList = []
for fruit in fruits:
newList.append(fruit.upper())
print(newList)
Anubis 6,51514 gold badges53 silver badges86 bronze badges answered Nov 6, 2015 at
10:08 CristinaCristina 691 silver badge1 bronze badge mylist = ['Mixed Case One', 'Mixed Case Two', 'Mixed Three']
print(list(map(lambda x: x.lower(), mylist)))
print(list(map(lambda x: x.upper(), mylist)))
TechJ 4972 gold badges5 silver badges14 bronze badges answered
Nov 26, 2009 at 5:24 ChiraelChirael 2,9354 gold badges27 silver badges28 bronze badges A much simpler version of the top answer is given
here by @Amorpheuses. With a list of values in val: valsLower = [item.lower() for item in vals]
This worked well for me with an f = open() text source. answered Nov 19, 2019 at 19:02 WhooNoWhooNo 7332 gold badges8 silver badges26 bronze badges 0 Solution: >>> s = []
>>> p = ['This', 'That', 'There', 'is', 'apple']
>>> [s.append(i.lower()) if not i.islower() else s.append(i) for i in p]
>>> s
>>> ['this', 'that', 'there', 'is','apple']
This solution will create a separate list containing the lowercase items, regardless of their original case. If the original case is upper then the list s will contain lowercase of the respective item in list p . If the original case of the list item is already lowercase in
list p then the list s will retain the item's case and keep it in lowercase. Now you can use list s instead of list p . ADyson 53.2k13 gold badges48 silver badges61 bronze badges answered May 31, 2018 at 8:24 SunilSunil 3411 gold badge7 silver badges24 bronze badges If your purpose is to matching with another string by converting in one pass, you can use str.casefold() as well. This is useful when you have non-ascii characters and matching with ascii versions(eg: maße vs masse).Though str.lower or str.upper fails in such cases, str.casefold() will pass. This is available in Python 3 and the idea is discussed in detail with
the answer https://stackoverflow.com/a/31599276/4848659. >>>str="Hello World";
>>>print(str.lower());
hello world
>>>print(str.upper());
HELLO WOLRD
>>>print(str.casefold());
hello world
answered Oct 16, 2018 at 0:18 GimhaniGimhani 1,14811 silver badges22 bronze badges You could try using: my_list = ['india', 'america', 'china', 'korea']
def capitalize_list(item):
return item.upper()
print(list(map(capitalize_list, my_list)))
U12-Forward 66k12 gold badges76 silver badges95 bronze badges answered Feb 9, 2020 at 15:50
Here's another solution to the problem, but I don't recommend using it. Just putting it here for completion of this topic since this solution wasn't added before. import timeit
def foo1():
L = ["A", "B", "C", "&"]
return [x.lower() for x in L]
def foo2():
L = ["A", "B", "C", "&"]
return "%".join(L).lower().split("%")
for i in range(10):
print("foo1", timeit.timeit(foo1, number=100000))
print("foo2", timeit.timeit(foo2, number=100000), end="\n\n")
foo1 0.0814619
foo2 0.058695300000000006
foo1 0.08401910000000004
foo2 0.06001100000000004
foo1 0.08252670000000001
foo2 0.0601641
foo1 0.08721100000000004
foo2 0.06254229999999994
foo1 0.08776279999999992
foo2 0.05946070000000003
foo1 0.08383590000000007
foo2 0.05982449999999995
foo1 0.08354679999999992
foo2 0.05930219999999997
foo1 0.08526650000000013
foo2 0.060690699999999875
foo1 0.09940110000000013
foo2 0.08484609999999981
foo1 0.09921800000000003
foo2 0.06182889999999985
answered Apr 25 at 8:21 If you are trying to convert all string to lowercase in the list, You can use pandas : import pandas as pd
data = ['Study', 'Insights']
pd_d = list(pd.Series(data).str.lower())
output: ['study', 'insights']
answered Oct 30, 2019 at 9:29 Aaditya UraAaditya Ura 11.1k7 gold badges44 silver badges75 bronze badges Not the answer you're looking for? Browse other questions tagged python list or ask your own
question. |