1

I'm trying to make all entries in my list dictionary lowercase but I cant figure out a way to do this

def get_entire_word_list():
dictionary = []
f = open("dictionary.txt", "r")
for x in f:
    dictionary.append(x.strip('\n'))

f.close()

for x in dictionary:
    x.lower()

for x in dictionary:
    print(x)

return dictionary

This just won't do it :(

I tried

for x in dictionary:
    x = x.lower()

but that wont do it either.

Thanks for any tips

macropod
  • 12,757
  • 2
  • 9
  • 21
  • Just call `lower` as you add the strings. `dictionary.append(x.strip('\n').lower())`. – chepner Jan 20 '22 at 19:51
  • `x = x.lower()` just re-assigns a local variable `x`, where the *new string* from `x.lower()` is assigned to. This does nothing to modify the list. To do this, you'd have to modify the list, or just create a new one. see the linked duplicate – juanpa.arrivillaga Jan 20 '22 at 19:51
  • @chepner AHH THANKS A LOT!! I tried that before but I put the .lower after the whole expression, like this: `dictionary.append(x.strip('\n')).lower()` That just threw an error – qwertzniki6 Jan 20 '22 at 19:56

1 Answers1

2

If you have a list of strings, called dictionary, you can use a list comprehension like

dictionary = [x.lower() for x in dictionary]

to create a new list that contains only lowered cased string.

Simon Hawe
  • 3,968
  • 6
  • 14