Python | List Element Count with Order
Last Updated :
25 Apr, 2023
Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element’s occurrence in that list. This type of problem can come across many domains. Let’s discuss a way in which this problem can be solved.
Method 1: Using defaultdict() + loop
We can perform this task using defaultdict() and loop by carefully assigning and incrementing order of elements.
Python3
from collections import defaultdict
test_list = [ 1 , 4 , 1 , 5 , 4 , 1 , 5 ]
print ( "The original list : " + str (test_list))
temp = defaultdict( int )
res = []
for ele in test_list:
temp[ele] + = 1
res.append((ele, temp[ele]))
print ( "List elements with their order count : " + str (res))
|
Output :
The original list : [1, 4, 1, 5, 4, 1, 5]
List elements with their order count : [(1, 1), (4, 1), (1, 2), (5, 1), (4, 2), (1, 3), (5, 2)]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using defaultdict() + loop which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method 2 : Using count() method, with slicing
Python3
test_list = [ 1 , 4 , 1 , 5 , 4 , 1 , 5 ]
print ( "The original list : " + str (test_list))
res = []
for i in range ( 0 , len (test_list)):
x = test_list[i]
a = test_list[:i].count(x) + 1
res.append((x, a))
print ( "List elements with their order count : " + str (res))
|
Output
The original list : [1, 4, 1, 5, 4, 1, 5]
List elements with their order count : [(1, 1), (4, 1), (1, 2), (5, 1), (4, 2), (1, 3), (5, 2)]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the res list
Method 3 : Using count() and append() methods
Python3
test_list = [ 1 , 4 , 1 , 5 , 4 , 1 , 5 ]
print ( "The original list : " + str (test_list))
res = []
x = []
for i in range ( 0 , len (test_list)):
x.append(test_list[i])
a = (test_list[i],x.count(test_list[i]))
res.append(a)
print ( "List elements with their order count : " + str (res))
|
Output
The original list : [1, 4, 1, 5, 4, 1, 5]
List elements with their order count : [(1, 1), (4, 1), (1, 2), (5, 1), (4, 2), (1, 3), (5, 2)]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n), where n is the number of elements in the res list
Method 4: using operator.countOf() method
Python3
import operator as op
test_list = [ 1 , 4 , 1 , 5 , 4 , 1 , 5 ]
print ( "The original list : " + str (test_list))
res = []
for i in range ( 0 , len (test_list)):
x = test_list[i]
a = op.countOf(test_list[:i], x) + 1
res.append((x, a))
print ( "List elements with their order count : " + str (res))
|
Output
The original list : [1, 4, 1, 5, 4, 1, 5]
List elements with their order count : [(1, 1), (4, 1), (1, 2), (5, 1), (4, 2), (1, 3), (5, 2)]
Time Complexity: O(N)
Auxiliary Space : O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...