0

Firstly, I am very very new to Python. I understand the basics of for loops used to iterate through a list, however, I am trying to replace an element that passes a basic equality check, which is this case== " ". When the iteration reaches a " ", it should replace the blank spot with a letter "A" and return a True.

However, this is not the case, it always returns a False :( If someone could find the time to point a very basic way to do this, it would be much appreciated. I have searched, but most of the answers include such things as enumerate and len functions etc, which is above my understanding thus far. Thank you for any assistance offered.

# my list to iterate through.
myList = ["A", "A", " ", "B", "B", "C", " ", "A", "B"]

# my function to check for conditionals
def checkBook(spots,grade):
    if spots[0] == grade and spots[1] == grade and spots[2] == grade:
        return True
    else:
        return False 

# my function to iterate through myList, then calls up the checkBook 
# function to get a return
def compareElements():
    for elements in myList:
        if elements == " ":
            elements = "A"
            print (checkBook(myList, "A"))

compareElements()
chowsai
  • 565
  • 3
  • 15
Amy K
  • 3
  • 1

2 Answers2

1

The error is here:

for elements in myList:
    if elements == " ":
        elements = "A"

In this case, you are only assigning "A" to the variable elements, and not modifying the original myList.

In this code below, myList[i] = "A" will modify myList, where i is the index of element, as enumerate will return the index and the item as you iterate through. (changed the variable name from elements to element to prevent confusion)

# my list to iterate through.
myList = ["A", "A", " ", "B", "B", "C", " ", "A", "B"]

# my function to check for conditionals
def checkBook(spots,grade):
    # if spots[0] == grade and spots[1] == grade and spots[2] == grade:
    if spots[0] == spots[1] == spots[2] == grade: # can be simplified to this
        return True
    else:
        return False 

# my function to iterate through myList, then calls up the checkBook 
# function to get a return
def compareElements():
    for i,element in enumerate(myList): # index, item
        if element == " ": 
            myList[i] = "A" # modifies myList
            print (checkBook(myList, "A"))

compareElements() # prints True, True
print(myList) # ['A', 'A', 'A', 'B', 'B', 'C', 'A', 'A', 'B']

Hope this helps :)

chowsai
  • 565
  • 3
  • 15
  • Thank you for your time, really great thorough explanation! While I have not learnt the use of enumerate yet, after some digging, I realize it's a much more tidy and easier way to utilize the "for..range" loop. I understand where my mistake was now, I did not modify myList as you suggested. Again, thankyou! – Amy K Sep 11 '17 at 01:14
0

You can try this:

myList = ["A", "A", " ", "B", "B", "C", " ", "A", "B"]
def checkBook(spots,grade):
     new_list = [grade if i == ' ' else i for i in spots]
     return new_list == myList, new_list

flag, final_list = checkBook(myList, "A")
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Thank you for your input, however, this is probably above my current understanding of Python. Through scouring the Internet, I'm going to assume you utilized List Comprehension? – Amy K Sep 11 '17 at 01:19
  • @AmyK Yes, list comprehension is utilized on line 3. – Ajax1234 Sep 11 '17 at 11:32