381

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?

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
user219126
  • 3,821
  • 2
  • 16
  • 4

13 Answers13

640

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']
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
YOU
  • 120,166
  • 34
  • 186
  • 219
  • 65
    Second proposition with **map** is correct but wasteful. There is **no point in making a lambda function**. Just use `map(str.lower, ["A","B","C"])` – fralau Jan 20 '18 at 09:12
  • 1
    When I try to print a list after this call nothing changes. Why is that? – mimic Jul 05 '18 at 17:51
  • 2
    @mimic A bit late, but for people coming across this, I'm guessing your issue was probably that you were not assigning the result of the list comprehension back to your list. Just doing to the list comprehension returns the value, but does not reassign it to the list variable. – Michael Kolber Jun 26 '19 at 14:56
  • 1
    What is the fastest between the two? (list comprehension vs map) – ThePhi Jun 28 '21 at 05:25
  • 3
    @ThePhi I did a quick check using a small sample I have. Doing a loop on my 20 lists with around 500 items each. The fastest was the former (avg. 0.0011s) vs using map (avg. 0.0022s). – Robin Carlo Catacutan Sep 18 '22 at 11:42
71
>>> list(map(str.lower,["A","B","C"]))
['a', 'b', 'c']
Amit JS
  • 133
  • 1
  • 7
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
67

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
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 5
    Do you know the reason behind why a list comprehension is faster than map? – Nixuz Nov 26 '09 at 06:05
  • 6
    It isn't always faster. Here's an example where it's not: http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map But it's not *much* slower in that case. Using a lambda obviously makes a big difference. There are more examples of why it's dangerous to trust your intuition on performance issues, especially in Python. – Ned Deily Nov 26 '09 at 07:50
  • 4
    in python 3, `map` wins the race, but does nothing :) – Jean-François Fabre Mar 07 '18 at 19:22
  • 2
    @NedDeily `map(str.lower,["A","B","C"])` is fastest is python3.7.5 – SHIVAM JINDAL Apr 28 '20 at 08:54
  • 1
    @SHIVAMJINDAL because `map` doesn't do anything besides create an object that does the `.lower()` when you *iterate* over it. If you try `list(map())` it's slower. Just as Jean-François said. – Boris Verkhovskiy Jun 09 '21 at 17:08
21

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']
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    err, using `list` as a variable name isn't the best choice :) – Jean-François Fabre Mar 07 '18 at 19:21
  • No but, since the name is of little importance to the method being shown, it's not really relevant. However, I'll change the name in case someone wants to use the code as is. – paxdiablo Mar 08 '18 at 07:17
  • the magic of stackoverflow: 250 votes for a python 2-only solution using lambda where it shouldn't!! well 249 now – Jean-François Fabre Mar 08 '18 at 08:12
  • @Jean-FrançoisFabre, not sure why you think this is this a Python-2-only solution. As the transcripts shows, it's *clearly* running under Python 3.5.2. In fact, I just checked it *again* for confirmation. ... some time passes while I investigate ... Actually, never mind, it appears you were talking about the current accepted answer rather than this one, so you probably should be commenting *there* rather than here. No doubt an honest mistake. Cheers. – paxdiablo Mar 08 '18 at 08:27
  • 1
    yeah, I wasn't criticizing yours (apart from the `list` stuff :)).Where do you think the UV you recently got comes from ? :) – Jean-François Fabre Mar 08 '18 at 08:38
7

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
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
6
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
  • 512
  • 2
  • 5
  • 16
Chirael
  • 3,025
  • 4
  • 28
  • 28
6

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,995
  • 14
  • 56
  • 87
Cristina
  • 69
  • 1
  • 1
6

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.

WhooNo
  • 911
  • 2
  • 12
  • 28
1

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
Gimhani
  • 1,318
  • 13
  • 23
1

You could try using:

my_list = ['india', 'america', 'china', 'korea']

def capitalize_list(item):
    return item.upper()

print(list(map(capitalize_list, my_list)))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
user10821509
  • 185
  • 1
  • 8
1

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
Alex Mortez
  • 156
  • 1
  • 8
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
  • 57,178
  • 14
  • 51
  • 63
Sunil
  • 429
  • 1
  • 9
  • 25
0

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']
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88