Reputation: 131
The contacts are added by other function to this lst = []
I´m building an Address Book with a function that allows you to Delete a contact from a list of lists by mentioning the name:
def deletec(lst):
name=str(input("Name:"))
lst = sum(lst, [])
names = lst[::3]
numbers = lst[1::3]
emails = lst[2::3]
dct = {name: (number,email) for name, number, email in zip(names, numbers, emails)}
if name in dct:
del dct[name]
print("Deleted!")
else:
print("Name Doesn´t Exists!")
list_key_value=[ [k,v] for k,v in dct.items()]
list_key_value = [[name, *tp] for (name, tp) in list_key_value]
lst=list_key_value
return lst
Which if your list is this:
[['Bruno', '44444', '[email protected]'], ['Mariah', '333', '[email protected]'], ['Oliver', '3333', '[email protected]']]
And you want to delete Bruno using only the function it returns this:
[['Mariah', '333', '[email protected]'], ['Oliver', '3333', '[email protected]']]
But when you ask another function that prints it by alphabetic order it uses the old lst:
def arrangebyorder(lst):
from operator import itemgetter
print(sorted(lst, key=itemgetter(0)))
return lst
Printing this:
[['Bruno', '44444', '[email protected]'], ['Mariah', '333', '[email protected]'], ['Oliver', '3333', '[email protected]']]
I was thinking of doing this to solve this problem, since list_key_value
is empty if you don´t delete any name from lst
you do a function where when this variable is empty prints the old lst
sorted alphabetically else if list_key_value
is defined you print list_key_value
sorted alphabetically:
def list_sorted(lst,lst_key_value):
try:
list_key_value
except NameError:
from operator import itemgetter
print(sorted(lst, key=itemgetter(0)))
return lst
else:
from operator import itemgetter
print(sorted(list_key_value, key=itemgetter(0)))
return list_key_value
The problem is that when I do it once lst_key_value is not defined it brings this error:
TypeError: list_sorted(lst,lst_key_value) missing 1 required positional argument: 'lst_key_value'
Can anybody think in other solution?
Only solutions that allow/work with List of Lists
Upvotes: 0
Views: 143
Reputation: 2386
Your code seems to be unnecessarily complicated. You can just use simple list comprehension to remove entries from your list of addresses. You should probably also introduce a class for your addresses, otherwise your code will quickly become hard to read. The following code seems to do everything that you want at much better readability:
class Address(object):
def __init__(self, name, number, email):
self.name = name
self.number = number
self.email = email
def __repr__(self):
return "Address(%s, %d, %s)" % (self.name, self.number, self.email)
# your list of addresses
addresses = [
Address('Bruno', 44444, '[email protected]'),
Address('Mariah', 333, '[email protected]'),
Address('Oliver', 3333, '[email protected]')
]
# Print your addresses
# prints: [Address(Bruno, 44444, [email protected]), Address(Mariah, 333, [email protected]), Address(Oliver, 3333, [email protected])]
print(addresses)
# Remove "Bruno" from your addresses
# replacement for your "deletec()" function
# prints: [Address(Mariah, 333, [email protected]), Address(Oliver, 3333, [email protected])]
print([address for address in addresses if address.name != "Bruno"])
# Sort your addresses descending by name
# replacement for your "arrangebyorder()" function
# prints: [Address(Oliver, 3333, [email protected]), Address(Mariah, 333, [email protected]), Address(Bruno, 44444, [email protected])]
print(sorted(addresses, key=lambda address: address.name, reverse=True))
Similar list of lists solution, as requested by your edit:
addresses = [
['Bruno', 44444, '[email protected]'],
['Mariah', 333, '[email protected]'],
['Oliver', 3333, '[email protected]']
]
# prints: [['Bruno', 44444, '[email protected]'], ['Mariah', 333, '[email protected]'], ['Oliver', 3333, '[email protected]']]
print(addresses)
# Remove "Bruno" from your addresses
# prints: [['Mariah', 333, '[email protected]'], ['Oliver', 3333, '[email protected]']]
print([address for address in addresses if address[0] != "Bruno"])
# Sort your addresses descending by name
# prints: [['Oliver', 3333, '[email protected]'], ['Mariah', 333, '[email protected]'], ['Bruno', 44444, '[email protected]']]
print(sorted(addresses, key=lambda address: address[0], reverse=True))
Upvotes: 3