Counting the number of items in the Python list

Asked 2 years ago, Updated 2 years ago, 46 views

num=[1,2,2,3,3,4,4,4,4,4] How can I count the number of duplicate entries in this list? The count function should not be used and should be counted using a for repetitive statement.

python list

2022-09-20 22:18

1 Answers

num = [1,2,2,3,3,4,4,4,4,4]

# # for statement
for n in num:
    print(n)

# # dictionary
d = { 'a': 1, 'b': 2 }

# # exist or not exist in a dictionary 
print('a' in d) # True
print('c' in d) # False
print('c' not in d) # True

# # set k-v to a dict
d['c'] = 3
print('c' in d) # True

# # if-else statement
if ('c' not in d):
    print('c is not exist in a dict')
else:
    # # get a value from a dict
    print(d['c']) # 3

# # increment
d['c'] += 1
print(d['c']) # 4

You can combine the individual grammar above.


2022-09-20 22:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.