Become a MacRumors Supporter for \$50/year with no ads, ability to filter front page stories, and private forums.

#### patent10021

##### macrumors 68040
Original poster
I'm looking for the Python equivalent of Swift's count method. array.count.

If the dataset has 9,000 data points I want to be able to find the average rating by dividing by the number of items (rows). Naturally I want to divide by items.count. How is this done in Python?

Code:
``````row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['WeChat', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Line', 0.0, 'USD', 1724546, 4.5]
row_5 = ['WhatsApp', 0.0, 'USD', 1126879, 4.0]

app_data_set = [row_1, row_2, row_3, row_4, row_5]

rating_sum = 0
for item in app_data_set:
rating = item[-1]
rating_sum += rating

items_count = item[-1].count() // python doesn't like this nor does it like count(item[-1])
average_rating = rating_sum / items_count
print(average_rating)``````

I know Python also has enumerate with a counter variable like idx. You can then use idx count value for other things like boolean tests.

Code:
``    for idx, item in enumerate(app_data_set):``

In Swift we can easily do what I'm trying to do just by using dot syntax and the count method but I cannot figure it out in Python.

Staff member

#### patent10021

##### macrumors 68040
Original poster
Thanks chown33, Thant worked a treat. I previously used the len() function to get the row count of a dataset, but thought I could use count() for lists since it actually does do counting. Problem is, count() only counts occurrences.

For anyone else who's interested, here's how len() works for arrays/lists just like Swift's count method.

Code:
``````for item in app_data_set:
rating = item[-1]
rating_sum += rating

ratings_count = len(app_data_set)
average_rating = rating_sum / ratings_count
print(average_rating)``````

Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.