2

I am a beginner in python and I would like your opinion on a more sophisticated way to write the code that follows. I want the code to iterate through the whole list (x) of items, count how many times each item appears and store the output in a new list (z). Here is the code (it works as it is) :

x = ["apple", "orange", "cherry", "apple"]

def new_list(a):
    z=[]
    for i in x:
        y = x.count(i)
        (z.append(y))
    return z

print(new_list(x)) 
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
alex
  • 21
  • 2

5 Answers5

2

I would suggest to do a list comprehension:

>>> x = ["apple", "orange", "cherry", "apple"]
>>> [x.count(val) for val in x]
[2, 1, 1, 2]
>>> 

Or even better with map:

>>> [*map(x.count, x)]
[2, 1, 1, 2]
>>> 
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

With using list.count() order of your programming is O(n^2) and with Counter and hashmap order of your programmer is O(n). Try this:

x = ["apple", "orange", "cherry", "apple"]

[x.count(i) for i in x]
# [2,1,1,2]

Or use Counter:

from collections import Counter
dct = Counter(x)
# Counter({'apple': 2, 'orange': 1, 'cherry': 1})

[dct[i] for i in x]
# [2, 1, 1, 2]

Check Runtime: (Counter is faster than list.count())

enter image description here

I'mahdi
  • 11,310
  • 3
  • 17
  • 23
0

Try using the Counter class:

from collections import Counter
x = ["foo", "bar", "foo", "foo"];
z = Counter(x)
print(z)
# Output: {"foo": 3, "bar": 1} 
Itamar Cohen
  • 123
  • 2
  • 11
0

You can use the Counter class in collections. So iterate over input list and then find count of every element.

from collections import Counter
x = ["apple", "orange", "cherry", "apple"]
c = Counter(x)
print([c[item] for item in x])
#[2, 1, 1, 2]
Albin Paul
  • 3,188
  • 2
  • 13
  • 28
0

You can use the Counter subclass of the collections module:

from collections import Counter

x = ["apple", "orange", "cherry", "apple"]


counter = Counter(x)
z = list(counter.values())

print(z) #[2,1,1]

I didn't use count because it may not be good for performance which is also pointed out by this answer.