how to add country codes to a list from a list of phone numbers in python

I need to create a script where I take a list of phone numbers, and be able to search through those phone numbers by using their area code. The only requirement is that I have to use slice. Any help would be appreciated!

phone_numbers = ["+1 709 239 5000", "+49 151 123 456", "+64 21 202 4961", "+679 324 4362", "+1 834 721 4975", "+1 257 534 2131", "+49 764 756 242", "+679 444 0193", "+49 555 555 5555"]
country_code = input("Search your international contacts by country code: ")
country_codes = []


for i in phone_numbers:
    number = str(phone_numbers.index(i))
    area_code = number["+", " "]
    print(area_code)

I tried to slice through the "+" and the first space, and add those to a list but I guess I messed up using slice.

EDIT: I guess I kinda suck at explaining what I wanted, but the person who answered got it basically spot on! I had to change it a little bit to get what I wanted but it works!

Code:

#### ---- SETUP ---- ####

phone_numbers = ["+1 709 239 5000", "+49 151 123 456", "+64 21 202 4961", "+679 324 4362", "+1 834 721 4975", "+1 257 534 2131", "+49 764 756 242", "+679 444 0193", "+49 555 555 5555"]
country_code = input("Search your international contacts by country code: ")

#### ---- CONTACT SEARCH ---- ####
for number_string in phone_numbers:
    plus_index = number_string.index('+')
    space_index = number_string.index(' ')
    cuntry = number_string[plus_index:space_index]
    if country_code in cuntry:
        print(number_string)

output: (inputted 49)

Search your international contacts by country code: 49
+49 151 123 456
+49 764 756 242
+49 555 555 5555

>Solution :

I think I understand what you want to do, but your current code doesn’t really do most of the steps correctly. number, in your code is going to be '0' on the first iteration, since i is the first string (index 0) in the phone_numbers list. number["+", " "] doesn’t make any sense at all, since you can’t use a tuple of strings as an index into another string. I think you want almost all the parts of your code in a different arrangement.

Try something more like:

phone_numbers = ["+1 709 239 5000", "+49 151 123 456", "+64 21 202 4961", "+679 324 4362", "+1 834 721 4975", "+1 257 534 2131", "+49 764 756 242", "+679 444 0193", "+49 555 555 5555"]

for number_string in phone_numbers:
    plus_index = number_string.index('+')
    space_index = number_string.index(' ')
    country_code = number_string[plus_index:space_index]
    # do whatever you want to do with country_code here, e.g. printing
    print(country_code)

If you don’t want the plus character to be part of the country code you’re saving, you could add one to the plus_index value when you do the slice.

Note that the code I’ve shown here doesn’t really match with the prompt you wrote to the user in your original code. If you want to search for a specific code, you probably don’t need to be doing the slicing you seemed to want. You could instead just check if query_country_code in number_string: and then do whatever you wanted to do in case of a match. The in operator does a substring search, and if you always give the country code with a + prefix, it should only match the actual country code, rather than some other random part of the string (you could add some validation steps to make sure the query was valid before starting the search, if necessary). You could also use str.startswith to only match a prefix string, rather than potentially finding any plus-prefixed substring anywhere in the larger number.

Leave a Reply