-1
string_to_sort = 'Sorting1234'

l = list(string_to_sort)

uppers = numbers = lowers = list() # this is the trouble, but dont know why

for char in l:
    if char.isupper():
        print("it's upper: " + char)
        uppers.append(char)
    elif char.islower():
        print("it's lower " + char)
        lowers.append(char)
    elif char.isdigit():
        print("it's digit: " + char)
        numbers.append(char)

print(uppers)
print(lowers)
print(numbers)

the 3 lists all have same content, but when I init-d the lists , I just made them empty, not related to each other. What's the reason all 3 lists get same content?

ERJAN
  • 22,540
  • 20
  • 65
  • 127

1 Answers1

1

You are creating single list and refer it from three variables. If you want three separate lists, initialize them separately:

uppers = list()
numbers = list()
lowers = list()
Marcin Orlowski
  • 68,918
  • 10
  • 117
  • 136