Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Kth digit Sum

Improve Article
Save Article
  • Last Updated : 28 Feb, 2023
Improve Article
Save Article

Given a Numeric list, extract the sum of the Kth digit.

Input : test_list = [5467, 34232, 45456, 22222, 3455], K = 2 
Output : 19 
Explanation : 6 + 2 + 4 + 2 + 5 = 19.

Input : test_list = [5467, 34232, 45456, 22222, 3455], K = 0 
Output : 17 
Explanation : 5 + 3 + 4 + 2 + 3 = 17. 

Method #1 : Using str() + loop

In this, we convert the element to string and then compute the summation of only Kth digit by extracting it using loop.

Python3




# Python3 code to demonstrate working of
# Kth digit Sum
# Using loop + sum()
 
# initializing list
test_list = [5467, 34232, 45456, 22222, 3455]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
res = 0
for ele in test_list:
 
    # adding Kth digit
    res += int(str(ele)[K])
 
# printing result
print("Kth digit sum : " + str(res))


Output:

The original list is : [5467, 34232, 45456, 22222, 3455]
Kth digit sum : 19

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using sum() + list comprehension + str()

In this, we perform the task of getting sum using sum(), and list comprehension is used to get one-liner approach to the problem. 

Python3




# Python3 code to demonstrate working of
# Kth digit Sum
# Using sum() + list comprehension + str()
 
# initializing list
test_list = [5467, 34232, 45456, 22222, 3455]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# sum() getting summation
res = sum([int(str(ele)[K]) for ele in test_list])
 
# printing result
print("Kth digit sum : " + str(res))


Output:

The original list is : [5467, 34232, 45456, 22222, 3455]
Kth digit sum : 19

Time Complexity: O(n)
Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!