Find most common string in a 2D list
I have a 2D list:
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
I want to find the most frequent element in the 2D list. In the above example, the most common string is 'Mohit'.
I know I can use brute force using two for loops and a dictionary to do this, but is there a more efficient way using numpy or any other library?
The nested lists could be of different lengths
Can someone also add the time of their methods? To find the fasted method. Also the caveats at which it might not be very efficient.
Edit
These are the timings of different methods on my system:
#timegb
%%timeit
collections.Counter(chain.from_iterable(arr)).most_common(1)[0][0]
5.91 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Kevin Fang and Curious Mind
%%timeit
flat_list = [item for sublist in arr for item in sublist]
collections.Counter(flat_list).most_common(1)[0]
6.42 µs ± 501 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
c = collections.Counter(item for sublist in arr for item in sublist).most_common(1)c[0][0]
6.79 µs ± 449 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Mayank Porwal
def most_common(lst):
return max(set(lst), key=lst.count)
%%timeit
ls = list(chain.from_iterable(arr))
most_common(ls)
2.33 µs ± 42.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#U9-Forward
%%timeit
l=[x for i in arr for x in i]
max(l,key=l.count)
2.6 µs ± 68.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Mayank Porwal's method runs the fastest on my system.
python python-3.x list numpy numpy-ndarray
|
show 5 more comments
I have a 2D list:
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
I want to find the most frequent element in the 2D list. In the above example, the most common string is 'Mohit'.
I know I can use brute force using two for loops and a dictionary to do this, but is there a more efficient way using numpy or any other library?
The nested lists could be of different lengths
Can someone also add the time of their methods? To find the fasted method. Also the caveats at which it might not be very efficient.
Edit
These are the timings of different methods on my system:
#timegb
%%timeit
collections.Counter(chain.from_iterable(arr)).most_common(1)[0][0]
5.91 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Kevin Fang and Curious Mind
%%timeit
flat_list = [item for sublist in arr for item in sublist]
collections.Counter(flat_list).most_common(1)[0]
6.42 µs ± 501 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
c = collections.Counter(item for sublist in arr for item in sublist).most_common(1)c[0][0]
6.79 µs ± 449 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Mayank Porwal
def most_common(lst):
return max(set(lst), key=lst.count)
%%timeit
ls = list(chain.from_iterable(arr))
most_common(ls)
2.33 µs ± 42.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#U9-Forward
%%timeit
l=[x for i in arr for x in i]
max(l,key=l.count)
2.6 µs ± 68.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Mayank Porwal's method runs the fastest on my system.
python python-3.x list numpy numpy-ndarray
How do you define 'frequent`, occurs most number of times or occurs in most number of sub-lists/rows?
– Kevin Fang
Nov 23 '18 at 5:46
Occurs most number of times in the complete 2d Array.
– Mohit Motwani
Nov 23 '18 at 5:47
Any constraints on number of elements in the nested array (n) vs. number of nested array (m). i.e. m >> n or n << m?
– bigdata2
Nov 23 '18 at 5:53
@bigdata2 not really. The 2D list is unlikely to be very big. Even the elements within the list.
– Mohit Motwani
Nov 23 '18 at 5:57
1
@MohitMotwani the timings kinda depend on the length of the list and the number of unique elements in it. The max(set... solution is fast for lists with few unique elements
– timgeb
Nov 23 '18 at 6:31
|
show 5 more comments
I have a 2D list:
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
I want to find the most frequent element in the 2D list. In the above example, the most common string is 'Mohit'.
I know I can use brute force using two for loops and a dictionary to do this, but is there a more efficient way using numpy or any other library?
The nested lists could be of different lengths
Can someone also add the time of their methods? To find the fasted method. Also the caveats at which it might not be very efficient.
Edit
These are the timings of different methods on my system:
#timegb
%%timeit
collections.Counter(chain.from_iterable(arr)).most_common(1)[0][0]
5.91 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Kevin Fang and Curious Mind
%%timeit
flat_list = [item for sublist in arr for item in sublist]
collections.Counter(flat_list).most_common(1)[0]
6.42 µs ± 501 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
c = collections.Counter(item for sublist in arr for item in sublist).most_common(1)c[0][0]
6.79 µs ± 449 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Mayank Porwal
def most_common(lst):
return max(set(lst), key=lst.count)
%%timeit
ls = list(chain.from_iterable(arr))
most_common(ls)
2.33 µs ± 42.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#U9-Forward
%%timeit
l=[x for i in arr for x in i]
max(l,key=l.count)
2.6 µs ± 68.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Mayank Porwal's method runs the fastest on my system.
python python-3.x list numpy numpy-ndarray
I have a 2D list:
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
I want to find the most frequent element in the 2D list. In the above example, the most common string is 'Mohit'.
I know I can use brute force using two for loops and a dictionary to do this, but is there a more efficient way using numpy or any other library?
The nested lists could be of different lengths
Can someone also add the time of their methods? To find the fasted method. Also the caveats at which it might not be very efficient.
Edit
These are the timings of different methods on my system:
#timegb
%%timeit
collections.Counter(chain.from_iterable(arr)).most_common(1)[0][0]
5.91 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Kevin Fang and Curious Mind
%%timeit
flat_list = [item for sublist in arr for item in sublist]
collections.Counter(flat_list).most_common(1)[0]
6.42 µs ± 501 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
c = collections.Counter(item for sublist in arr for item in sublist).most_common(1)c[0][0]
6.79 µs ± 449 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Mayank Porwal
def most_common(lst):
return max(set(lst), key=lst.count)
%%timeit
ls = list(chain.from_iterable(arr))
most_common(ls)
2.33 µs ± 42.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#U9-Forward
%%timeit
l=[x for i in arr for x in i]
max(l,key=l.count)
2.6 µs ± 68.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Mayank Porwal's method runs the fastest on my system.
python python-3.x list numpy numpy-ndarray
python python-3.x list numpy numpy-ndarray
edited Nov 23 '18 at 12:46
timgeb
50.2k116390
50.2k116390
asked Nov 23 '18 at 5:36
Mohit Motwani
1,1251422
1,1251422
How do you define 'frequent`, occurs most number of times or occurs in most number of sub-lists/rows?
– Kevin Fang
Nov 23 '18 at 5:46
Occurs most number of times in the complete 2d Array.
– Mohit Motwani
Nov 23 '18 at 5:47
Any constraints on number of elements in the nested array (n) vs. number of nested array (m). i.e. m >> n or n << m?
– bigdata2
Nov 23 '18 at 5:53
@bigdata2 not really. The 2D list is unlikely to be very big. Even the elements within the list.
– Mohit Motwani
Nov 23 '18 at 5:57
1
@MohitMotwani the timings kinda depend on the length of the list and the number of unique elements in it. The max(set... solution is fast for lists with few unique elements
– timgeb
Nov 23 '18 at 6:31
|
show 5 more comments
How do you define 'frequent`, occurs most number of times or occurs in most number of sub-lists/rows?
– Kevin Fang
Nov 23 '18 at 5:46
Occurs most number of times in the complete 2d Array.
– Mohit Motwani
Nov 23 '18 at 5:47
Any constraints on number of elements in the nested array (n) vs. number of nested array (m). i.e. m >> n or n << m?
– bigdata2
Nov 23 '18 at 5:53
@bigdata2 not really. The 2D list is unlikely to be very big. Even the elements within the list.
– Mohit Motwani
Nov 23 '18 at 5:57
1
@MohitMotwani the timings kinda depend on the length of the list and the number of unique elements in it. The max(set... solution is fast for lists with few unique elements
– timgeb
Nov 23 '18 at 6:31
How do you define 'frequent`, occurs most number of times or occurs in most number of sub-lists/rows?
– Kevin Fang
Nov 23 '18 at 5:46
How do you define 'frequent`, occurs most number of times or occurs in most number of sub-lists/rows?
– Kevin Fang
Nov 23 '18 at 5:46
Occurs most number of times in the complete 2d Array.
– Mohit Motwani
Nov 23 '18 at 5:47
Occurs most number of times in the complete 2d Array.
– Mohit Motwani
Nov 23 '18 at 5:47
Any constraints on number of elements in the nested array (n) vs. number of nested array (m). i.e. m >> n or n << m?
– bigdata2
Nov 23 '18 at 5:53
Any constraints on number of elements in the nested array (n) vs. number of nested array (m). i.e. m >> n or n << m?
– bigdata2
Nov 23 '18 at 5:53
@bigdata2 not really. The 2D list is unlikely to be very big. Even the elements within the list.
– Mohit Motwani
Nov 23 '18 at 5:57
@bigdata2 not really. The 2D list is unlikely to be very big. Even the elements within the list.
– Mohit Motwani
Nov 23 '18 at 5:57
1
1
@MohitMotwani the timings kinda depend on the length of the list and the number of unique elements in it. The max(set... solution is fast for lists with few unique elements
– timgeb
Nov 23 '18 at 6:31
@MohitMotwani the timings kinda depend on the length of the list and the number of unique elements in it. The max(set... solution is fast for lists with few unique elements
– timgeb
Nov 23 '18 at 6:31
|
show 5 more comments
5 Answers
5
active
oldest
votes
- Flatten the list with
itertools.chain.from_iterable
- Apply a
Counter.
Demo:
>>> from itertools import chain
>>> from collections import Counter
>>>
>>> lst = [['Mohit', 'shini','Manoj','Mot'],
...: ['Mohit', 'shini','Manoj'],
...: ['Mohit', 'Vis', 'Nusrath']]
...:
>>> Counter(chain.from_iterable(lst)).most_common(1)[0][0]
'Mohit'
Details:
>>> list(chain.from_iterable(lst))
['Mohit',
'shini',
'Manoj',
'Mot',
'Mohit',
'shini',
'Manoj',
'Mohit',
'Vis',
'Nusrath']
>>> Counter(chain.from_iterable(lst))
Counter({'Manoj': 2, 'Mohit': 3, 'Mot': 1, 'Nusrath': 1, 'Vis': 1, 'shini': 2})
>>> Counter(chain.from_iterable(lst)).most_common(1)
[('Mohit', 3)]
Some timings:
>>> lst = lst*100
>>> %timeit Counter(chain.from_iterable(lst)).most_common(1)[0][0] # timgeb
53.7 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit max([x for i in lst for x in i], key=l.count) # U9-Forward
207 µs ± 389 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit Counter([x for sublist in lst for x in sublist]).most_common(1)[0][0] # Curious_Mind/Kevin Fang #1
75.2 µs ± 2.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit Counter(item for sublist in lst for item in sublist).most_common(1)[0][0] # Kevin Fang #2
95.2 µs ± 2.07 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit flat = list(chain.from_iterable(lst)); max(set(flat), key=flat.count) # Mayank Porwal
98.4 µs ± 178 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(Note that Kevin Fang's second solution is a bit slower than the first one, but more memory efficient.)
1
Yeah looks likechainis the fastest flatten method from this question
– Kevin Fang
Nov 23 '18 at 5:58
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
1
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
add a comment |
I'd suggest flatten out the 2D Array and then use a counter to find out the most frequent element.
flat_list = [item for sublist in arr for item in sublist]
from collections import Counter
Counter(flat_list).most_common(1)[0]
# ('Mohit', 3)
Counter(flat_list).most_common(1)[0][0]
# 'Mohit'
Not sure if it is the fastest approach though.
Edit:
@timgeb's answer has a faster way to flatten the list using itertools.chain
A more space efficient way suggested by @schwobaseggl:
from collections import Counter
c = Counter(item for sublist in arr for item in sublist).most_common(1)
# [('Mohit', 3)]
c[0][0]
# 'Mohit'
1
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
add a comment |
One way to do it this way,
import collections
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
c = collections.Counter([x for sublist in arr for x in sublist])
print(c.most_common(1) )
print("--- %s seconds ---" % (time.time() - start_time))
Time taken: 0.00016713142395 seconds
DEMO: http://tpcg.io/NH3zjm
1
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
1
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
add a comment |
Something like this:
In [920]: from itertools import chain
In [923]: arr = list(chain.from_iterable(arr)) ## flatten into 1-D array
In [922]: def most_common(lst):
...: return max(set(lst), key=lst.count)
In [924]: most_common(arr)
Out[924]: 'Mohit'
Timings:
from itertools import chain
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
arr = list(chain.from_iterable(arr))
arr = arr*100
def most_common(lst):
return max(set(lst), key=lst.count)
print(most_common(arr))
print("--- %s seconds ---" % (time.time() - start_time))
mayankp@mayank:~$ python t1.py
Mohit
--- 0.000154972076416 seconds ---
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@MohitMotwani Edited the timings too. Also notearr= arr*100in my code.
– Mayank Porwal
Nov 23 '18 at 6:40
add a comment |
Or why not:
l=[x for i in arr for x in i]
max(l,key=l.count)
Code example:
>>> arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
>>> l=[x for i in arr for x in i]
>>> max(l,key=l.count)
'Mohit'
>>>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441133%2ffind-most-common-string-in-a-2d-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Flatten the list with
itertools.chain.from_iterable
- Apply a
Counter.
Demo:
>>> from itertools import chain
>>> from collections import Counter
>>>
>>> lst = [['Mohit', 'shini','Manoj','Mot'],
...: ['Mohit', 'shini','Manoj'],
...: ['Mohit', 'Vis', 'Nusrath']]
...:
>>> Counter(chain.from_iterable(lst)).most_common(1)[0][0]
'Mohit'
Details:
>>> list(chain.from_iterable(lst))
['Mohit',
'shini',
'Manoj',
'Mot',
'Mohit',
'shini',
'Manoj',
'Mohit',
'Vis',
'Nusrath']
>>> Counter(chain.from_iterable(lst))
Counter({'Manoj': 2, 'Mohit': 3, 'Mot': 1, 'Nusrath': 1, 'Vis': 1, 'shini': 2})
>>> Counter(chain.from_iterable(lst)).most_common(1)
[('Mohit', 3)]
Some timings:
>>> lst = lst*100
>>> %timeit Counter(chain.from_iterable(lst)).most_common(1)[0][0] # timgeb
53.7 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit max([x for i in lst for x in i], key=l.count) # U9-Forward
207 µs ± 389 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit Counter([x for sublist in lst for x in sublist]).most_common(1)[0][0] # Curious_Mind/Kevin Fang #1
75.2 µs ± 2.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit Counter(item for sublist in lst for item in sublist).most_common(1)[0][0] # Kevin Fang #2
95.2 µs ± 2.07 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit flat = list(chain.from_iterable(lst)); max(set(flat), key=flat.count) # Mayank Porwal
98.4 µs ± 178 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(Note that Kevin Fang's second solution is a bit slower than the first one, but more memory efficient.)
1
Yeah looks likechainis the fastest flatten method from this question
– Kevin Fang
Nov 23 '18 at 5:58
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
1
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
add a comment |
- Flatten the list with
itertools.chain.from_iterable
- Apply a
Counter.
Demo:
>>> from itertools import chain
>>> from collections import Counter
>>>
>>> lst = [['Mohit', 'shini','Manoj','Mot'],
...: ['Mohit', 'shini','Manoj'],
...: ['Mohit', 'Vis', 'Nusrath']]
...:
>>> Counter(chain.from_iterable(lst)).most_common(1)[0][0]
'Mohit'
Details:
>>> list(chain.from_iterable(lst))
['Mohit',
'shini',
'Manoj',
'Mot',
'Mohit',
'shini',
'Manoj',
'Mohit',
'Vis',
'Nusrath']
>>> Counter(chain.from_iterable(lst))
Counter({'Manoj': 2, 'Mohit': 3, 'Mot': 1, 'Nusrath': 1, 'Vis': 1, 'shini': 2})
>>> Counter(chain.from_iterable(lst)).most_common(1)
[('Mohit', 3)]
Some timings:
>>> lst = lst*100
>>> %timeit Counter(chain.from_iterable(lst)).most_common(1)[0][0] # timgeb
53.7 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit max([x for i in lst for x in i], key=l.count) # U9-Forward
207 µs ± 389 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit Counter([x for sublist in lst for x in sublist]).most_common(1)[0][0] # Curious_Mind/Kevin Fang #1
75.2 µs ± 2.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit Counter(item for sublist in lst for item in sublist).most_common(1)[0][0] # Kevin Fang #2
95.2 µs ± 2.07 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit flat = list(chain.from_iterable(lst)); max(set(flat), key=flat.count) # Mayank Porwal
98.4 µs ± 178 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(Note that Kevin Fang's second solution is a bit slower than the first one, but more memory efficient.)
1
Yeah looks likechainis the fastest flatten method from this question
– Kevin Fang
Nov 23 '18 at 5:58
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
1
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
add a comment |
- Flatten the list with
itertools.chain.from_iterable
- Apply a
Counter.
Demo:
>>> from itertools import chain
>>> from collections import Counter
>>>
>>> lst = [['Mohit', 'shini','Manoj','Mot'],
...: ['Mohit', 'shini','Manoj'],
...: ['Mohit', 'Vis', 'Nusrath']]
...:
>>> Counter(chain.from_iterable(lst)).most_common(1)[0][0]
'Mohit'
Details:
>>> list(chain.from_iterable(lst))
['Mohit',
'shini',
'Manoj',
'Mot',
'Mohit',
'shini',
'Manoj',
'Mohit',
'Vis',
'Nusrath']
>>> Counter(chain.from_iterable(lst))
Counter({'Manoj': 2, 'Mohit': 3, 'Mot': 1, 'Nusrath': 1, 'Vis': 1, 'shini': 2})
>>> Counter(chain.from_iterable(lst)).most_common(1)
[('Mohit', 3)]
Some timings:
>>> lst = lst*100
>>> %timeit Counter(chain.from_iterable(lst)).most_common(1)[0][0] # timgeb
53.7 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit max([x for i in lst for x in i], key=l.count) # U9-Forward
207 µs ± 389 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit Counter([x for sublist in lst for x in sublist]).most_common(1)[0][0] # Curious_Mind/Kevin Fang #1
75.2 µs ± 2.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit Counter(item for sublist in lst for item in sublist).most_common(1)[0][0] # Kevin Fang #2
95.2 µs ± 2.07 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit flat = list(chain.from_iterable(lst)); max(set(flat), key=flat.count) # Mayank Porwal
98.4 µs ± 178 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(Note that Kevin Fang's second solution is a bit slower than the first one, but more memory efficient.)
- Flatten the list with
itertools.chain.from_iterable
- Apply a
Counter.
Demo:
>>> from itertools import chain
>>> from collections import Counter
>>>
>>> lst = [['Mohit', 'shini','Manoj','Mot'],
...: ['Mohit', 'shini','Manoj'],
...: ['Mohit', 'Vis', 'Nusrath']]
...:
>>> Counter(chain.from_iterable(lst)).most_common(1)[0][0]
'Mohit'
Details:
>>> list(chain.from_iterable(lst))
['Mohit',
'shini',
'Manoj',
'Mot',
'Mohit',
'shini',
'Manoj',
'Mohit',
'Vis',
'Nusrath']
>>> Counter(chain.from_iterable(lst))
Counter({'Manoj': 2, 'Mohit': 3, 'Mot': 1, 'Nusrath': 1, 'Vis': 1, 'shini': 2})
>>> Counter(chain.from_iterable(lst)).most_common(1)
[('Mohit', 3)]
Some timings:
>>> lst = lst*100
>>> %timeit Counter(chain.from_iterable(lst)).most_common(1)[0][0] # timgeb
53.7 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit max([x for i in lst for x in i], key=l.count) # U9-Forward
207 µs ± 389 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit Counter([x for sublist in lst for x in sublist]).most_common(1)[0][0] # Curious_Mind/Kevin Fang #1
75.2 µs ± 2.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit Counter(item for sublist in lst for item in sublist).most_common(1)[0][0] # Kevin Fang #2
95.2 µs ± 2.07 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit flat = list(chain.from_iterable(lst)); max(set(flat), key=flat.count) # Mayank Porwal
98.4 µs ± 178 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(Note that Kevin Fang's second solution is a bit slower than the first one, but more memory efficient.)
edited Nov 23 '18 at 6:36
answered Nov 23 '18 at 5:56
timgeb
50.2k116390
50.2k116390
1
Yeah looks likechainis the fastest flatten method from this question
– Kevin Fang
Nov 23 '18 at 5:58
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
1
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
add a comment |
1
Yeah looks likechainis the fastest flatten method from this question
– Kevin Fang
Nov 23 '18 at 5:58
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
1
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
1
1
Yeah looks like
chain is the fastest flatten method from this question– Kevin Fang
Nov 23 '18 at 5:58
Yeah looks like
chain is the fastest flatten method from this question– Kevin Fang
Nov 23 '18 at 5:58
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
@timegb Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:08
1
1
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@MohitMotwani all the timings need to be done on the same computer because only then they are comparable. I will do some timings in a minute.
– timgeb
Nov 23 '18 at 6:09
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@timegb Really appreciate it. Thank you
– Mohit Motwani
Nov 23 '18 at 6:10
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
@MohitMotwani sorry, messed up some timings... edited again
– timgeb
Nov 23 '18 at 6:36
add a comment |
I'd suggest flatten out the 2D Array and then use a counter to find out the most frequent element.
flat_list = [item for sublist in arr for item in sublist]
from collections import Counter
Counter(flat_list).most_common(1)[0]
# ('Mohit', 3)
Counter(flat_list).most_common(1)[0][0]
# 'Mohit'
Not sure if it is the fastest approach though.
Edit:
@timgeb's answer has a faster way to flatten the list using itertools.chain
A more space efficient way suggested by @schwobaseggl:
from collections import Counter
c = Counter(item for sublist in arr for item in sublist).most_common(1)
# [('Mohit', 3)]
c[0][0]
# 'Mohit'
1
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
add a comment |
I'd suggest flatten out the 2D Array and then use a counter to find out the most frequent element.
flat_list = [item for sublist in arr for item in sublist]
from collections import Counter
Counter(flat_list).most_common(1)[0]
# ('Mohit', 3)
Counter(flat_list).most_common(1)[0][0]
# 'Mohit'
Not sure if it is the fastest approach though.
Edit:
@timgeb's answer has a faster way to flatten the list using itertools.chain
A more space efficient way suggested by @schwobaseggl:
from collections import Counter
c = Counter(item for sublist in arr for item in sublist).most_common(1)
# [('Mohit', 3)]
c[0][0]
# 'Mohit'
1
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
add a comment |
I'd suggest flatten out the 2D Array and then use a counter to find out the most frequent element.
flat_list = [item for sublist in arr for item in sublist]
from collections import Counter
Counter(flat_list).most_common(1)[0]
# ('Mohit', 3)
Counter(flat_list).most_common(1)[0][0]
# 'Mohit'
Not sure if it is the fastest approach though.
Edit:
@timgeb's answer has a faster way to flatten the list using itertools.chain
A more space efficient way suggested by @schwobaseggl:
from collections import Counter
c = Counter(item for sublist in arr for item in sublist).most_common(1)
# [('Mohit', 3)]
c[0][0]
# 'Mohit'
I'd suggest flatten out the 2D Array and then use a counter to find out the most frequent element.
flat_list = [item for sublist in arr for item in sublist]
from collections import Counter
Counter(flat_list).most_common(1)[0]
# ('Mohit', 3)
Counter(flat_list).most_common(1)[0][0]
# 'Mohit'
Not sure if it is the fastest approach though.
Edit:
@timgeb's answer has a faster way to flatten the list using itertools.chain
A more space efficient way suggested by @schwobaseggl:
from collections import Counter
c = Counter(item for sublist in arr for item in sublist).most_common(1)
# [('Mohit', 3)]
c[0][0]
# 'Mohit'
edited Nov 23 '18 at 6:12
answered Nov 23 '18 at 5:52
Kevin Fang
1,266316
1,266316
1
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
add a comment |
1
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
1
1
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
You might also be more space-efficient by using a nested generator expression instead of a list. Rather store the counter in a variable as you need it either way, while you do not need the list.
– schwobaseggl
Nov 23 '18 at 6:02
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
@KevinFang Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:07
add a comment |
One way to do it this way,
import collections
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
c = collections.Counter([x for sublist in arr for x in sublist])
print(c.most_common(1) )
print("--- %s seconds ---" % (time.time() - start_time))
Time taken: 0.00016713142395 seconds
DEMO: http://tpcg.io/NH3zjm
1
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
1
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
add a comment |
One way to do it this way,
import collections
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
c = collections.Counter([x for sublist in arr for x in sublist])
print(c.most_common(1) )
print("--- %s seconds ---" % (time.time() - start_time))
Time taken: 0.00016713142395 seconds
DEMO: http://tpcg.io/NH3zjm
1
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
1
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
add a comment |
One way to do it this way,
import collections
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
c = collections.Counter([x for sublist in arr for x in sublist])
print(c.most_common(1) )
print("--- %s seconds ---" % (time.time() - start_time))
Time taken: 0.00016713142395 seconds
DEMO: http://tpcg.io/NH3zjm
One way to do it this way,
import collections
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
c = collections.Counter([x for sublist in arr for x in sublist])
print(c.most_common(1) )
print("--- %s seconds ---" % (time.time() - start_time))
Time taken: 0.00016713142395 seconds
DEMO: http://tpcg.io/NH3zjm
edited Nov 23 '18 at 6:22
answered Nov 23 '18 at 5:57
Curious_Mind
14.6k32443
14.6k32443
1
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
1
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
add a comment |
1
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
1
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
1
1
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@Curios_Mind Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
1
1
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
@MohitMotwani EDITED
– Curious_Mind
Nov 23 '18 at 6:23
add a comment |
Something like this:
In [920]: from itertools import chain
In [923]: arr = list(chain.from_iterable(arr)) ## flatten into 1-D array
In [922]: def most_common(lst):
...: return max(set(lst), key=lst.count)
In [924]: most_common(arr)
Out[924]: 'Mohit'
Timings:
from itertools import chain
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
arr = list(chain.from_iterable(arr))
arr = arr*100
def most_common(lst):
return max(set(lst), key=lst.count)
print(most_common(arr))
print("--- %s seconds ---" % (time.time() - start_time))
mayankp@mayank:~$ python t1.py
Mohit
--- 0.000154972076416 seconds ---
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@MohitMotwani Edited the timings too. Also notearr= arr*100in my code.
– Mayank Porwal
Nov 23 '18 at 6:40
add a comment |
Something like this:
In [920]: from itertools import chain
In [923]: arr = list(chain.from_iterable(arr)) ## flatten into 1-D array
In [922]: def most_common(lst):
...: return max(set(lst), key=lst.count)
In [924]: most_common(arr)
Out[924]: 'Mohit'
Timings:
from itertools import chain
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
arr = list(chain.from_iterable(arr))
arr = arr*100
def most_common(lst):
return max(set(lst), key=lst.count)
print(most_common(arr))
print("--- %s seconds ---" % (time.time() - start_time))
mayankp@mayank:~$ python t1.py
Mohit
--- 0.000154972076416 seconds ---
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@MohitMotwani Edited the timings too. Also notearr= arr*100in my code.
– Mayank Porwal
Nov 23 '18 at 6:40
add a comment |
Something like this:
In [920]: from itertools import chain
In [923]: arr = list(chain.from_iterable(arr)) ## flatten into 1-D array
In [922]: def most_common(lst):
...: return max(set(lst), key=lst.count)
In [924]: most_common(arr)
Out[924]: 'Mohit'
Timings:
from itertools import chain
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
arr = list(chain.from_iterable(arr))
arr = arr*100
def most_common(lst):
return max(set(lst), key=lst.count)
print(most_common(arr))
print("--- %s seconds ---" % (time.time() - start_time))
mayankp@mayank:~$ python t1.py
Mohit
--- 0.000154972076416 seconds ---
Something like this:
In [920]: from itertools import chain
In [923]: arr = list(chain.from_iterable(arr)) ## flatten into 1-D array
In [922]: def most_common(lst):
...: return max(set(lst), key=lst.count)
In [924]: most_common(arr)
Out[924]: 'Mohit'
Timings:
from itertools import chain
import time
start_time = time.time()
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
arr = list(chain.from_iterable(arr))
arr = arr*100
def most_common(lst):
return max(set(lst), key=lst.count)
print(most_common(arr))
print("--- %s seconds ---" % (time.time() - start_time))
mayankp@mayank:~$ python t1.py
Mohit
--- 0.000154972076416 seconds ---
edited Nov 23 '18 at 6:40
answered Nov 23 '18 at 5:54
Mayank Porwal
4,4991624
4,4991624
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@MohitMotwani Edited the timings too. Also notearr= arr*100in my code.
– Mayank Porwal
Nov 23 '18 at 6:40
add a comment |
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@MohitMotwani Edited the timings too. Also notearr= arr*100in my code.
– Mayank Porwal
Nov 23 '18 at 6:40
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
Thank you. This works. Can you also add the time taken by your method? It'd be easier to check the fastest method.
– Mohit Motwani
Nov 23 '18 at 6:09
@MohitMotwani Edited the timings too. Also note
arr= arr*100 in my code.– Mayank Porwal
Nov 23 '18 at 6:40
@MohitMotwani Edited the timings too. Also note
arr= arr*100 in my code.– Mayank Porwal
Nov 23 '18 at 6:40
add a comment |
Or why not:
l=[x for i in arr for x in i]
max(l,key=l.count)
Code example:
>>> arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
>>> l=[x for i in arr for x in i]
>>> max(l,key=l.count)
'Mohit'
>>>
add a comment |
Or why not:
l=[x for i in arr for x in i]
max(l,key=l.count)
Code example:
>>> arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
>>> l=[x for i in arr for x in i]
>>> max(l,key=l.count)
'Mohit'
>>>
add a comment |
Or why not:
l=[x for i in arr for x in i]
max(l,key=l.count)
Code example:
>>> arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
>>> l=[x for i in arr for x in i]
>>> max(l,key=l.count)
'Mohit'
>>>
Or why not:
l=[x for i in arr for x in i]
max(l,key=l.count)
Code example:
>>> arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
>>> l=[x for i in arr for x in i]
>>> max(l,key=l.count)
'Mohit'
>>>
answered Nov 23 '18 at 5:59
U9-Forward
13.3k21237
13.3k21237
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441133%2ffind-most-common-string-in-a-2d-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you define 'frequent`, occurs most number of times or occurs in most number of sub-lists/rows?
– Kevin Fang
Nov 23 '18 at 5:46
Occurs most number of times in the complete 2d Array.
– Mohit Motwani
Nov 23 '18 at 5:47
Any constraints on number of elements in the nested array (n) vs. number of nested array (m). i.e. m >> n or n << m?
– bigdata2
Nov 23 '18 at 5:53
@bigdata2 not really. The 2D list is unlikely to be very big. Even the elements within the list.
– Mohit Motwani
Nov 23 '18 at 5:57
1
@MohitMotwani the timings kinda depend on the length of the list and the number of unique elements in it. The max(set... solution is fast for lists with few unique elements
– timgeb
Nov 23 '18 at 6:31