0

I have list items as below. Want to delete particular list within this list. When user enter phone number, it check with list and get particular list which have matched phone number list and then delete that particular list and display rest of list.

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

c_phone = int(input("Enter your phone to Close Account : "))
    for a, b, c, d in account:
        if c_phone == b:
           del account[b]
           print(account)

the result will be:

Enter your phone to Close Account : 9852560352

[['abc', 8566665891, 's', 5000], ['pqr', 6854265891, 's', 7000]]
2
  • Please be a bit more specific when asking a question: What have you tried so far with a code example? (I downvoted because there is no code) / What do you expect? / What error do you get? For Help take a look at "How to ask" Commented Mar 4, 2019 at 8:39
  • You can use del x[i] to delete the element with index i from list x. The list is destructively modified. More generally, you can do del x[i:j:k] to delete a slice from list x. Commented Mar 4, 2019 at 8:42

5 Answers 5

1

Rebuilding the list with filter is probably better, but here is an answer with actual deletion of element:

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

c_phone = int(input("Enter your phone to Close Account : "))
for a, b, c, d in account:
    if c_phone == b:
       account.remove([a,b,c,d])
       print(account)
Sign up to request clarification or add additional context in comments.

Comments

1

You can filter your list with a listcomp:

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

number = 8566665891
account = [i for i in account if i[1] != number]
# [['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

Alternatively you can use the function filter():

list(filter(lambda x: x[1] != number, account))

You can also use a dict of lists instead of a list of lists. Removing of items in a dict is faster than in a list.

account = {j: [i, k, l] for i, j, k, l in account}
account.pop(number) # del account[number]

print(account)
{9852560352: ['xyz', 'c', 6000], 6854265891: ['pqr', 's', 7000]}

3 Comments

thanks for response, index will not work if k[1] != c_phone because user can enter any phone number. Above I have mentioned the phone just for reference
@evil007 Why not? The answer, which you accepted returns the same result. It is the index of the sublist and not of the list.
because user can enter any phone number and if we have big list then its deficult to define particular index. Above I have mentioned the phone just for reference
0

You can use enumerate while iterating over your list and then use del account[i] to delete the element with index i from your list.

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

# c_phone = int(input("Enter your phone to Close Account : "))
c_phone = 8566665891
for i, (a, b, c, d) in enumerate(account):
    if c_phone == b:
        del account[i]
        print(account)

Comments

0

Try this :

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]
c_phone = int(input("Enter your phone to Close Account : "))
print( [k for k in account if k[1] != c_phone] )

1 Comment

thanks for response, index will not work if k[1] != c_phone because user can enter any phone number. Above I have mentioned the phone just for reference.
0

You can enumerate on list so that it will index in idx and element in i you can check if item exists in list like if 8566665891 in i and delete the item by index

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]
c_phone = int(input("Enter your phone to Close Account : "))
for idx,i in enumerate(account):
     if c_phone in i:
          del account[idx]

print(account)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.