0

I'm trying to delete a tuple from my list, I've tried everything people have said but still no luck. I tried two methods, one time it removes the record even if it's not the name I want to remove, and the second doesn't remove at all.

record=[]
newrecord=[]
full_time=""
choice = ""
while (choice != "x"):
    print()
    print("a. Add a new employee")
    print("b. Display all employees")
    print("c. Search for an employee record")
    print("d. Delete an employee record")


    elif choice == "d":
        delete = str(input("Enter the name of the employee you would like to remove from the record: "))
        for d in record:
            if d == delete:
                record.remove(delete)

This doesn't remove anything.

If I change it to:

elif choice == "d":
        delete = str(input("Enter the name of the employee you would like to remove from the record: "))
        record = [n for n in record if delete in record]

It removes all if I do it this way.

Heres how i add to the list

choice = input("Choose an option (a to f) or x to Exit: ")

if choice == "a":
    full_name = str(input("Enter your name: ")).title()
    job_title = str(input("Enter your job title: ")).title()

while full_time.capitalize() != "Y" or full_time.capitalize() != "N":
    full_time=input("Do you work full time (Y/N): ").upper()


    if full_time.capitalize() == "Y":
        break
    elif full_time.capitalize() == "N":
        break
        break

hourly_rate = float(input("Enter your hourly rate: £"))

number_years = int(input("Enter the number of full years service: "))

record.append((full_name, job_title, full_time, "%.2f" % hourly_rate, number_years))
Xrin
  • 87
  • 1
  • 3
  • 15
  • 1
    The loop in the first example can be simplified to `if delete in record: record.remove(delete)` – Colonel Thirty Two Dec 04 '15 at 18:28
  • I updated the OP to show how i add to the record – Xrin Dec 04 '15 at 18:29
  • If you are looking to delete tuples, what element in the tuple matches the employee name? The whole tuple is not going to be equal to an employee name. Also, you can't just delete tuples from a list while iterating over it, see [Loop "Forgets" to Remove Some Items](https://stackoverflow.com/q/17299581) – Martijn Pieters Dec 04 '15 at 18:30

1 Answers1

1

Given that the name is the first element in the record any checks against the name must be done against the first element of the tuple.

Previously you had:

record = [n for n in record if delete in record]

The first problem here is that you have to check against n and not record in your condition:

record = [n for n in record if delete in n]

The next issue is that this will only add a record to the list if delete is found within it. It seems as though you want the inverse of this:

record = [n for n in record if delete not in n]
                                      ^^^

Now that in itself will not work because delete is a string and n is a tuple here, so we must combine this with the first fix. We then get:

record = [n for n in record if delete not in n[0]]

One thing I would note however is that if you are only using employee names as indexes it's probably much cleaner/easier to just use a dictionary with the employee name as keys and the other information as values. Given that a dictionary is an associative mapping between keys and values and your problem is exactly that I'd recommend changing your data structures.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • I understand what u mean, Im looking for the record position 0, however this still removes all the records? – Xrin Dec 04 '15 at 18:36
  • I just had a couple of typo's in the answer, see the edit. Honestly though I think using a dictionary or a class that keeps all the information together is the better option here. – shuttle87 Dec 04 '15 at 18:44