flask sqlalchemy remove duplicates 1

flask sqlalchemy remove duplicates

mylist = db.session.query(User.name).all() # get all user.name from db
mylist1 = len(list(dict.fromkeys(mylist))) # remove the duplicate then use len() to find total

Here is what the above code is Doing:
1. mylist = db.session.query(User.name).all()
– This is a query that gets all the User.name from the database.
– The result is a list of tuples.
– Each tuple contains a single value, which is the name of a user.
– The list of tuples looks like this:
[(‘John’,), (‘Jane’,), (‘John’,), (‘Jane’,), (‘John’,), (‘Jane’,)]
2. mylist1 = len(list(dict.fromkeys(mylist)))
– This is a list comprehension that converts the list of tuples into a list of strings.
– The list of strings looks like this:
[‘John’, ‘Jane’, ‘John’, ‘Jane’, ‘John’, ‘Jane’]
– The list comprehension looks like this:
[name[0] for name in mylist]
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name)
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name[0])
– The list comprehension is equivalent to this:
mylist2 = []
for name in mylist:
mylist2.append(name

Similar Posts