Home > Software engineering >  Better way to iterate through list and store output in a new list
Better way to iterate through list and store output in a new list

Time:09-17

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)) 

CodePudding user response:

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]
>>> 

CodePudding user response:

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

CodePudding user response:

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]

CodePudding user response:

Try using the Counter class:

From collections import Counter
x = ["foo", "bar", "foo", "foo"];
z = Counter(x)
print(z)
# Output: {"foo": 3, "bar": 1} 

CodePudding user response:

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.

  • Related