0

In the for cycle there is a function to remove all duplicates, but it doesn't. Also the .upper sometimes doesn't work. Please help.

from collections import OrderedDict
def ask():
  global x
  x = str(input("Enter your logical operation:"))
  x = x.split( )
  x = [x.upper() for x in x]
  return x

ask()

for i in range(0,len(x)):
  and_index = x.index("AND",i)
  n = list(OrderedDict.fromkeys(str(and_index)))
  print(n)

print(n)

Duarte Castanho
  • 305
  • 2
  • 6
  • 20
oguh43
  • 85
  • 8

2 Answers2

1

Example to remove duplicate from list

mylist = ["a", "b", "c", "a", "b"]
newlist = list(set(mylist))
print(newlist)

output : ["a", "b", "c"]

user341143
  • 448
  • 4
  • 12
1

First of all you're not even saving output of your ask() function, but instead serving it globally, that's not a good practice.

def ask():
  x = input("Enter your logical operation:")
  return list(map(str.upper, x.split()))

user_input = ask()

Then to simply remove duplicates you could convert your list to set, doing so will loose order of our list but will quickly and easly remove duplicates

user_input = list(set(user_input))

In case you want to keep order of operations entered by user you could use OrderedDict

from collections import OrderedDict
user_input = list(OrderedDict.fromkeys(user_input))
Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22