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
# Python3 code to demonstrate working of
# List Element Count Order
# using defaultdict() + loop
from collections import defaultdict
# initialize list
test_list = [1, 4, 1, 5, 4, 1, 5]
# printing original list
print("The original list : " + str(test_list))
# List Element Count Order
# using defaultdict() + loop
temp = defaultdict(int)
res = []
for ele in test_list:
temp[ele] += 1
res.append((ele, temp[ele]))
# printing result
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
# Python3 code to demonstrate working of
# List Element Count Order
# initialize list
test_list = [1, 4, 1, 5, 4, 1, 5]
# printing original list
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))
# printing result
print("List elements with their order count : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# List Element Count Order
# initialize list
test_list = [1, 4, 1, 5, 4, 1, 5]
# printing original list
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)
# printing result
print("List elements with their order count : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# List Element Count Order
import operator as op
# initialize list
test_list = [1, 4, 1, 5, 4, 1, 5]
# printing original list
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))
# printing result
print("List elements with their order count : " + str(res))
OutputThe 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)
Similar Reads
Python - Count elements in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read
Comparing Python Lists Without Order Sometimes we need to compare two lists without worrying about the order of elements. This is particularly useful when checking if two lists contain the same elements regardless of their arrangement. In this article, we'll explore different ways to compare Python lists without considering order.Using
3 min read
Python | Counting Nth tuple element Sometimes, while working with Python, we can have a problem in which we need to count the occurrence of a particular's elements. This kind of problem is quite common while working with records. Let's discuss a way in which this task can be performed. Method #1 : Using Counter() + generator expressio
5 min read
Python Tuple count() Method In this article, we will learn about the count() method used for tuples in Python. The count() method of a Tuple returns the number of times the given element appears in the tuple. Example Python3 tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) Output : 3Python Tuple count() Method Syntax
3 min read
Python | Count occurrence of all elements of list in a tuple Given a tuple and a list as input, write a Python program to count the occurrences of all items of the list in the tuple. Examples: Input : tuple = ('a', 'a', 'c', 'b', 'd') list = ['a', 'b'] Output : 3 Input : tuple = (1, 2, 3, 1, 4, 6, 7, 1, 4) list = [1, 4, 7] Output : 6 Approach #1 : Naive Appro
5 min read
Python - Count elements in record tuple Sometimes, while working with data in form of records, we can have a problem in which we need to find the total element counts of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method
5 min read
Python | Tuple Column element frequency In Python, we need to handle various forms of data and one among them is a list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the Kth element in the list of tuples. Letâs discuss certain ways in which this can
5 min read
Python List Counting Programs Python provides various methods, such as count(), dictionary-based counting, and list comprehensions, to efficiently handle counting operations. This collection of Python programs covers different ways to count elements in lists, including counting occurrences, matching elements, frequency analysis,
2 min read
Python - Count frequency of Sublist in given list Given a List and a sublist, count occurrence of sublist in list. Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation :
3 min read
Python - Reorder for consecutive elements Given a List perform reordering to get similar elements in consecution. Input : test_list = [4, 7, 5, 4, 1, 4, 1, 6, 7, 5] Output : [4, 4, 4, 7, 7, 5, 5, 1, 1, 6] Explanation : All similar elements are assigned to be consecutive. Input : test_list = [4, 7, 5, 1, 4, 1, 6, 7, 5] Output : [4, 4, 7, 7,
4 min read