Rir8
Rir8

Reputation: 39

Why does != not work in this case and are there other options to say two things are not equal or should I just try and restart my code?

I am trying to write a code for something as described below, but when I try and use it, a the shell returns a message (see below) saying I can't use the != because list indices must be integers or slices, not str, but when I do something like ['a'] != ['b'] or ['a', 'c'] != ['a', 'c'] in the shell, it works (first returns true and second returns false) and those are string lists so I'm not entirely sure what the problem is. Maybe the way I am writing the code makes it wrong or I don't need to use it all?

Why do I receive an error and are there alternatives to != or should I rewrite my code? (If my code seems completely off, it may be, I am a beginner and tried to construct it all on my own, I think/hope I'm headed in the right direction)

Code:

 def remove_match(list1, list2):
'''(list, list) -> list
Given two lists of a single character strings, return a new list
that contains only the characters in list1 that were not the same string
and in the same position in list 2. Both list1 and list2 
have the same length.
>>>remove_match(['P', 'O', 'Y', 'R'], ['P', 'B', 'G', 'R'])
['O', 'Y']
>>>remove_match(['P', 'G', 'G', 'R'], ['P', 'G', 'G', 'R'])
[]
>>>remove_match(['O', 'R', 'B', 'Y'], ['P', 'P', 'P', 'P'])
['O', 'R', 'B', 'Y']
'''
edit_list1=[]
for i in list1:
    if list1[i] != list2[i]:
        edit_list1.append(list1[i])
return edit_list1

The message that comes up when I try and use the function (such as one of the doc string examples ) is:

  Traceback (most recent call last):
  Python Shell, prompt 2, line 1
  File "/Users/Rir/Desktop/folder/file.py", line 56, in <module>
    if list1[i] != list2[i]:
 builtins.TypeError: list indices must be integers or slices, not str

Thanks!

Upvotes: 0

Views: 69

Answers (4)

f5r5e5d
f5r5e5d

Reputation: 3706

you probably wanted an incrementing integer index i

def remove_match(list1, list2):
    edit_list1=[]
    for i in range(len(list1)):  # get indices from range the length of your list
        if list1[i] != list2[i]:
            edit_list1.append(list1[i])
    return edit_list1   

zip() is good for "parallel" operations on lists:

def remove_match(list1, list2):
    edit_list1=[]
    for a, b in zip(list1, list2):  # zip lists together, get elements in pairs
        if a != b:                  # test on the paired elements, no indexing
            edit_list1.append(a)
    return edit_list1

and as shown in other answers you loop matches the List Comprehension pattern

def remove_match(list1, list2):
    return [a for a, b in zip(list1, list2) if a != b]  

Upvotes: 1

ajenie
ajenie

Reputation: 21

First of all, list indices must be integer. You can use enumerate() to get the index and the content at the same time. Then run it through loop.

However, I suggest you to use list comprehension.

list1 = ['P', 'O', 'Q', 'Y']
list2 = ['P', 'O', 'Q', 'A']

list_nomatch = [x for x in list1 if x not in list2]

This will produce 'Y'. Hope that helps

Upvotes: 2

Dust_In_The_Wind
Dust_In_The_Wind

Reputation: 3692

Since both lists are of the same size, you can use enumerate() -

def remove_match(list1, list2):
  edit_list1=[]
  for idx, i in enumerate(list1):
    if i != list2[idx]:
      edit_list1.append(i)
  return edit_list1

print(remove_match(['P', 'O', 'Y', 'R'], ['P', 'B', 'G', 'R']))
print(remove_match(['P', 'G', 'G', 'R'], ['P', 'G', 'G', 'R']))
print(remove_match(['O', 'R', 'B', 'Y'], ['P', 'P', 'P', 'P']))

Output -

['O', 'Y']
[]
['O', 'R', 'B', 'Y']

The same code using List Comprehension -

def remove_match(list1, list2):
  return [i for idx, i in enumerate(list1) if i != list2[idx]]

Upvotes: 0

Nae
Nae

Reputation: 15335

i in list1 corresponds to a value, not an index.

What you really want to do is probably:

edit_list1=[]
for i in list1:
    if i != list2[list1.index(i)]:
        edit_list1.append(i)
return edit_list1

Upvotes: 0

Related Questions