Sher Muhammad
Sher Muhammad

Reputation: 23

python adding into list sorted aphabetically

I have a list like this

entries = [{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, 
           {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, 
           {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

I want another list element to add in this list but at the position sorted by "Last Name"

Upvotes: 1

Views: 77

Answers (3)

Abhics202
Abhics202

Reputation: 19

def myFunc(e):
  return e['Last Name']

entries = [{'First Name': 'Sher', 'Last Name': 'Sello', 'Age': '22', 'Telephone': '2989484'},
           {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
           {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]
entries.append({'First Name': 'Sam', 'Last Name': 'Singh', 'Age': '22', 'Telephone': '3343434'})
entries.sort(key=myFunc)
print(entries)

Upvotes: 0

Sushil
Sushil

Reputation: 5531

You can append the new dictionary to the list and sort the list based on the Last Name. Here is how you do it:

entries = [{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

new = {'First Name': 'Dean', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '2989484'}
entries.append(new)

entries = sorted(entries, key = lambda i: i['Last Name'])
print(entries)

Output:

[{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Dean', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

But this might be time consuming as @Serial Lazer rightly pointed out in the comments. A better solution would be this:

entries = [{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

new = {'First Name': 'Dean', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '2989484'}

for index,dictt in enumerate(entries):
    if dictt['Last Name'] > new['Last Name']:
        entries.insert(index,new)
        break

print(entries)

Output:

[{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Dean', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

If you want to insert a list of dictionaries into entries, then the above solutions are not recommended as they are slower. The most efficient way would be to use the bisect module, like this:

import bisect

entries = [{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

new = [{'First Name': 'Dean', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '2989484'}]

for dictt in new:
    index = bisect.bisect([dict_['Last Name'] for dict_ in entries], dictt['Last Name'])
    entries.insert(index,dictt)

print(entries)

Output:

[{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Dean', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

Upvotes: 2

Ironkey
Ironkey

Reputation: 2607

if you want to sort every time you add info

you can also modify this to take non int values, but they seemed practical

entries = [{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]

def addData(first, last, age: int, phone: int):
    entries.append({'First Name': first, 'Last Name': last, 'Age': age, 'Telephone': phone})
    entries.sort(key = lambda x: x['Last Name'])

addData("iron", "key", 18, 1231231234)

print(entries)
[{'First Name': 'Sher', 'Last Name': 'Hello', 'Age': '22', 'Telephone': '2989484'}, {'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'}, {'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}, {'First Name': 'iron', 'Last Name': 'key', 'Age': 18, 'Telephone': 1231231234}]

Upvotes: 0

Related Questions