This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 4 years ago.
I have a list (ex: [1, 2, 2, 3, 4, 3]) and I need to only keep the first occurrence of any of the elements in the list (ex: the list should become [1, 2 ,3, 4]). I know that I could do something like this:
badList = [1, 2, 2, 3, 4, 3]
goodList = []
for element in badList:
if element in goodList:
continue
goodList.append(element)
print (goodList)
However, this is a very messy solution, and I am hoping there is a more elegant way.
from collections import OrderedDict
list(OrderedDict.fromkeys(badList))
[1, 2, 3, 4]
credit to #poke
Just convert to a set then back to a list:
goodList = list(set(badList))
Related
This question already has answers here:
Sorting a List by frequency of occurrence in a list
(7 answers)
Closed 1 year ago.
I have a problem, to sort the python list based on occurrence of elements.
for example:
#input list:
l = [5,5,5,2,2,4,4,4,4,1]
#output list
[4, 4, 4, 4, 5, 5, 5, 2, 2, 1]
I am able to solve the problem but i have used 3 loops, can anybody help me to do this in some pythonic way using a single loop. if you want i can share my solution. Thanks
You could use collections.Counter to count elements, then sort by that
>>> from collections import Counter
>>> l = [5,5,5,2,2,4,4,4,4,1]
>>> c = Counter(l)
>>> sorted(l, key=lambda i: c[i], reverse=True)
[4, 4, 4, 4, 5, 5, 5, 2, 2, 1]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 2 years ago.
I would like to remove a list from inside another list. I have a list which looks like this:
[[1, 2, 3, 4, 5, 6]]
I would like the list to look like this, so it is only a singular list rather than nested:
[1, 2, 3, 4, 5, 6]
To achieve the desired result, you can just access the zero index from original list.
a = [[1, 2, 3, 4, 5, 6]]
>>> b = a[0]
>>> b
// [1, 2, 3, 4, 5, 6]
If your list is :
lst = [[1,2,3,4,5,6]]
then you can do like this
new_lst = lst[0]
You new_lst would be :
[1,2,3,4,5,6]
If your original lst has many lists inside it like :
lst = [[1,2,3,4,5,6],[7,8,9,10],...]
Then you can do the following :
new_lst = []
for i in lst :
for j in i :
new_lst.append(j)
So your new_lst would be :
[1,2,3,4,5,6,7,8,9,10...]
You could also do it in a shorthand notation as :
new_list = [element for sublist in lst for element in sublist]
The above snippet is exactly similar as previous one, just a shorthand notation
This question already has answers here:
Removing duplicates in lists
(56 answers)
One-liner to remove duplicates, keep ordering of list [duplicate]
(6 answers)
Closed 3 years ago.
I have a big list of strings appearing many times and I want a list of the same strings to appear only once.
An example with numbers would be:
a = [1, 2, 2, 3, 4, 4]
and I want to get
b = [1, 2, 3, 4]
What I tried is something like:
a = [1, 2, 2, 3, 4, 4]
[x for x in a if a.count(x) == 1]
[1, 3]
but this omits the duplicate numbers and takes only those appearing once.
You can try this:
import collections
a = [1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8]
print([item for item, count in collections.Counter(a).items()])
This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 4 years ago.
def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
And this was the result of the query [0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]
The query is taking a user inputted term ('qtext'), tokenizing and generating a postings list for each token.
The posting list is a list of nested dictionaries (e.g. [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]. I am attempting to find the intersection for these nested dictionaries using the keys
Assuming the order does not matter, you could use set.intersection():
>>> lst = [[0, 2, 3, 4, 6], [1, 4, 5], [0, 2, 4], [4, 5]]
>>> set.intersection(*map(set,lst))
{4}
>>> set(lst[0]).intersection(*lst[1:])
{4}
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
Suppose I have a list of lists, like [[1, 2], [3, 4], [5, 6], [7, 8]]. What is the most elegant way in python to get [1, 2, 3, 4, 5, 6, 7, 8]?
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
Or:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
res=[]
for item in mylistOfList:
res+=item