0

I want to only find the values in the list which are strings, and change them to upper case.

If lst is not a list I want to return -1.

For the below example, the output should be: [11, 'TEST', 3.14, 'CASE'].

lst =  [11, 'TeSt', 3.14, 'cAsE']
def upper_strings(lst):
    if type(lst)!= list:
        lst = -1
    else:
        for char in lst:
            if type(char) == str:
                char=char.upper()
            else:
                char == char
    return lst
print(upper_strings(lst))

The output that I got was:

[11, 'TeSt', 3.14, 'cAsE']
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
rrr
  • 3
  • 2

1 Answers1

0

Try something like this:

def upper_strings(lst):   
  if not isinstance(lst, list):
    return -1 
  else:
    return [x.upper() if isinstance(x, str) else x for x in lst]
print(upper_strings(lst))

See here for information on list comprehensions: if else in a list comprehension

Zach Cleary
  • 458
  • 1
  • 4
  • 17
  • ok, that's good thank you but I don't want to change the char that are not str to -1 I want to leave them as they are, only if the input list is not a list I want to return -1 – rrr Nov 25 '19 at 15:24
  • Modified, please check again. – Zach Cleary Nov 25 '19 at 15:31