Increment Numeric Strings by K – Python
Given the Strings list we have to increment numeric strings in a list by a given integer K
. ( Note that strings that are not numeric remain unchanged )
For example: In the list ["gfg", "234", "is", "98", "123", "best", "4"]
if K = 6
then result will be ["gfg", "240", "is", "104", "129", "best", "10"]
.
Using List Comprehension and isdigit()
This method uses a list comprehension to process each element in the list (li
) and checks if an element is numeric using isdigit(), f
or numeric elements it converts them to integers then increments by K
and converts back to a string before adding to the result list (res
).
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K and keep non-numeric elements unchanged
res = [str(int(s) + K) if s.isdigit() else s for s in li]
print(res)
Output
['gfg', '240', 'is', '104', '129', 'best', '10']
Let’s take a look at other methods to Increment Numeric Strings by K.
Table of Content
Using isdigit()
This method processes each element in a list (li
) to check if it’s numeric using isdigit()
. If it is then the element is converted to an integer and incremented by K
before being added to the result list res
.
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
res = []
for e in li:
if e.isdigit():
res.append(str(int(e) + k))
else:
res.append(e)
print(res)
Output
Incremented Numeric Strings: ['gfg', '240', 'is', '104', '129', 'best', '10']
Using filter()
and map()
Functions
This method separates numeric and non-numeric elements from the list (li
) using the filter()
function then processes numeric elements by converting them to integers and incrementing them by K
before converting back to strings using map()
. Finally it combines the non-numeric elements with the updated numeric ones to form the final result (res
).
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Separate numeric and non-numeric elements, increment numeric elements by K
num1 = list(map(lambda s: str(int(s) + k), filter(str.isdigit, li)))
num2 = list(filter(lambda s: not s.isdigit(), li))
res = num1 + num2
print(res)
Output
['240', '104', '129', '10', 'gfg', 'is', 'best']
Explanation: filter()
function identifies numeric elements using str.isdigit
and non-numeric elements using a lambda function then numeric elements are processed by map()
, where each element is converted to an integer, incremented, and converted back to a string.
Using isnumeric()
Method
This method uses the isnumeric()
method to check if a string element is numeric then for numeric elements it converts them to integers and increments by K
before converting back to strings.
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K
res = [str(int(s) + k) if s.isnumeric() else s for s in li]
print(res)
Output
['gfg', '240', 'is', '104', '129', 'best', '10']
Using map()
and lambda
This method uses map()
with a lambda
function to check if an element in the list is numeric then for numeric elements it converts them to integers and increments by K
.
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K using map and lambda
res = list(map(lambda s: str(int(s) + k) if s.isdigit() else s, li))
print(res)
Output
['gfg', '240', 'is', '104', '129', 'best', '10']
Using try/except
This method uses a try/except block to handle each element then tries to convert the element to an integer and if successful it increments the value by K. Also note when elements can’t be converted it catches the ValueError and keeps them unchanged.
li = ["gfg", "234", "is", "98", "123", "best", "4"]
k = 6
# Increment numeric elements by K using try/except
res = []
for s in li:
try:
res.append(str(int(s) + k))
except ValueError:
res.append(s)
print(res)
Output
['gfg', '240', 'is', '104', '129', 'best', '10']