Code Explanation:
๐น 1. Creating the List
lst = [1,2,2,3,3,3]
๐ A list is created containing numbers.
๐ Some numbers are repeated:
1 → 1 time
2 → 2 times
3 → 3 times
๐น 2. Creating an Empty Dictionary
freq = {}
๐ An empty dictionary is created.
๐ This will store:
key = number
value = count (frequency)
๐น 3. Loop Through Each Element
for x in lst:
๐ The loop runs through each item in the list one by one:
First 1, then 2, 2, 3, 3, 3
๐น 4. Counting Logic (Main Step)
freq[x] = freq.get(x,0) + 1
๐ This is the most important line:
freq.get(x, 0) → gets current count of x
If x is not present → returns 0
Then +1 increases the count
Step-by-step:
First 1 → {1:1}
First 2 → {1:1, 2:1}
Second 2 → {1:1, 2:2}
First 3 → {1:1, 2:2, 3:1}
Second 3 → {1:1, 2:2, 3:2}
Third 3 → {1:1, 2:2, 3:3}
๐น 5. Printing the Result
print(freq)
๐ Displays the final dictionary
✅ Final Output:
{1: 1, 2: 2, 3: 3}

0 Comments:
Post a Comment