> I suggest that when you need this functionality you create your own data
> structure combining a list and a collections.Counter and keep track of
> this yourself.

I concur with the usage of collections.Counter here. Storing the count for
every single item in a list could end up being rather redundant with
duplicate elements, so Counter ends up being much more space efficient.
With it being implemented as a dictionary, the lookup speeds are incredibly
quick for getting an on-demand count for any item within it.

But, is it practically beneficial to combine the functionality of a list
and collections.Counter instead of just using both data structures together?

Here's a rough example of what I'm talking about. I recall using something
vaguely similar to this before:

```
items = [...]
counter = collections.Counter(items)
for item in items:
    # use counter[item] instead of items.count(item)
    if count := counter[item] > x:
        # do something with item and its count
```

(Note: omit "count :=" if the count isn't needed for anything beyond the
conditional check)

On Wed, Jan 15, 2020 at 10:34 AM Eric V. Smith <e...@trueblade.com> wrote:

> On 1/14/2020 11:03 PM, Hunter Jones wrote:
> > Hey everyone,
> >
> > I recently used list.count() in a coding interview and the question
> > arose about how scale-able this solution was for sufficiently large
> > input. Currently, list.count iterates through the list, incrementing
> > the count as it goes and returning the value at the end. This does not
> > lend itself well to large inputs.
> >
> > I propose either modifying the list struct to include a map of item ->
> > count, or implementing a new structure with this information that is
> > allocated separately when a list is instantiated.
> >
> > Maybe I'm just seeing this since it's an edge case that I recently
> > dealt with, but I believe it would be a useful enhancement overall.
> >
> > Thoughts?
>
> That seems like a pretty niche requirement. In all of my years using
> Python I've never needed such a thing.
>
> I suggest that when you need this functionality you create your own data
> structure combining a list and a collections.Counter and keep track of
> this yourself.
>
> Eric
>
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/QX7V3BFBVJBARTP6E3NSSAG3XQDDZ7FX/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XXHVXJWPRLAPOUCMXC2U33NP6NUJ5RK2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to