1

I would like to turn everything into a lowercase but no matter what i try it always brings back "AttributeError: 'list' object has no attribute 'lower'" what am i doing wrong?

user_input = input("")
    prohibited = {'this','although','and','as','because','but','even if','he','and','however','an','a','is','what','question :','question','[',']',',','cosmos',' ','  ','   ','cosmo'}
    tokens = [word for word in re.split("\W+", user_input) if word.lower() not in prohibited]
    tokens.sort(key=len, reverse=True)
    words = str(tokens).split()
    TokenCount = len(words)
    tokens = tokens.lower()
4
  • lower is a string method. tokens is a list of strings. Commented Aug 1, 2019 at 4:40
  • yes.. is there a way around that? Commented Aug 1, 2019 at 4:43
  • you can use map to use the lower for every element in list Commented Aug 1, 2019 at 4:44
  • tokens=list(map(lambda x:x.lower(),tokens)) Commented Aug 1, 2019 at 4:44

2 Answers 2

2

You can add a lambda expression to map the str.lower() calls to elements of list. Instead of your last line, try this:

tokens = list(map(lambda x: x.lower(), tokens))
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with a one line for loop:

tokens = [t.lower() for t in tokens]

This applies the string operation 'lower()' to each element of the list and then compiles them again into a list (the [] brackets).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.