Adding area code 84 only to phone numbers missing an area code in python

When working with phone numbers in Python, it is common to encounter cases where some phone numbers are missing an area code. In such situations, it becomes necessary to add the missing area code to ensure consistency and accuracy in data. In this article, we will explore three different ways to solve this problem using Python.

Option 1: Using Regular Expressions

Regular expressions provide a powerful way to search and manipulate strings in Python. We can leverage regular expressions to identify phone numbers missing an area code and add the desired area code to them.

import re

def add_area_code(phone_numbers, area_code):
    pattern = r'^d{7}$'  # Matches phone numbers with 7 digits
    updated_numbers = []
    
    for number in phone_numbers:
        if re.match(pattern, number):
            updated_numbers.append(area_code + number)
        else:
            updated_numbers.append(number)
    
    return updated_numbers

phone_numbers = ['1234567', '9876543', '5555555']
area_code = '84'

updated_numbers = add_area_code(phone_numbers, area_code)
print(updated_numbers)

In this solution, we define a regular expression pattern that matches phone numbers with exactly 7 digits. We iterate over the given list of phone numbers and check if each number matches the pattern. If it does, we add the area code to the number; otherwise, we leave it unchanged. The updated list of phone numbers is then returned.

Option 2: Using String Manipulation

If regular expressions seem too complex for the task at hand, we can achieve the same result using simple string manipulation techniques in Python.

def add_area_code(phone_numbers, area_code):
    updated_numbers = []
    
    for number in phone_numbers:
        if len(number) == 7:
            updated_numbers.append(area_code + number)
        else:
            updated_numbers.append(number)
    
    return updated_numbers

phone_numbers = ['1234567', '9876543', '5555555']
area_code = '84'

updated_numbers = add_area_code(phone_numbers, area_code)
print(updated_numbers)

In this approach, we iterate over the given list of phone numbers and check if each number has a length of 7 digits. If it does, we add the area code to the number; otherwise, we leave it unchanged. The updated list of phone numbers is then returned.

Option 3: Using List Comprehension

List comprehension is a concise and elegant way to perform operations on lists in Python. We can utilize list comprehension to solve this problem efficiently.

def add_area_code(phone_numbers, area_code):
    return [area_code + number if len(number) == 7 else number for number in phone_numbers]

phone_numbers = ['1234567', '9876543', '5555555']
area_code = '84'

updated_numbers = add_area_code(phone_numbers, area_code)
print(updated_numbers)

In this solution, we use a list comprehension to iterate over the given list of phone numbers. If a number has a length of 7 digits, we add the area code to it; otherwise, we leave it unchanged. The resulting list of phone numbers is then returned.

After evaluating all three options, it is evident that the third option, using list comprehension, is the most concise and efficient solution. It achieves the desired result in a single line of code, making it easier to read and maintain. Therefore, option 3 is the recommended approach for adding the area code 84 to phone numbers missing an area code in Python.

Rate this post

7 Responses

    1. Totally with you on that one! List comprehension is the way to go for coding ninjas like us. Its elegant, efficient, and shows off our skills. Keep rocking those 🤘💻 moves!

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents