Question

Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a...

Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a list of the integers that can be obtained by converting the strings to integers using an expression like int(string). Strings which cannot be converted in this way are ignored.

Hint: the statement pass is a statement that does nothing when executed.

For example:

Test Result
strings = ['123', '-39', '+45', 'x', 'COSC121', '123+', '12-3']
print(valid_integers(strings))
[123, -39, 45]
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def valid_integers(lst):
    result = []
    for x in lst:
        try:
            result.append(int(x))
        except Exception:
            continue
    return result


# Testing
strings = ['123', '-39', '+45', 'x', 'COSC121', '123+', '12-3']
print(valid_integers(strings))

Output :

[123, -39:45]

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!
Add a comment
Know the answer?
Add Answer to:
Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function named "find_key" that takes a key-value store as a parameter with strings as...

    Write a function named "find_key" that takes a key-value store as a parameter with strings as keys and integers as values. The function returns a boolean representing true if the string "focus" is in the input as a key, false otherwise(javascript)

  • Complete the get_mid_letter() function which is passed a list of strings as a parameter. The function...

    Complete the get_mid_letter() function which is passed a list of strings as a parameter. The function returns a string made up of the concatenation of the middle letter of each word from the parameter list. The string returned by the function should be in lowercase characters. If the parameter list is an empty list, the function should return an empty string For example Test Result print("1.", get mid_letter"Jess", "Cain", Amity", "Raeann"])) 1. siia Answer (penalty regime: 0 %) 1 -Idef...

  • solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as...

    solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as a parameter and returns the count of strings that have at least the length of second parameter passed into the function that are found in the list. Recall that you can determine whether an item is a string by writing type(item) == str. The only list functions you are allowed to use are len(), indexing (lst[i] for an integer i), or slicing (lst[i:j] for...

  • Write a function rm_multiple() that takes two parameters as input (a list of strings, and a...

    Write a function rm_multiple() that takes two parameters as input (a list of strings, and a list of strings to remove from the list) and returns a new list with the items removed. # Define function in this cell def rm_multiple(strings,remove_list): l = [10,20,40,50] #use to delete an element in the list for string in strings: strings= l if string not in remove_list: lst.append(string) # Edit the code here to call your function in this cell items = [10,20,40,50] to_remove...

  • Write a function named "csv_to_list" that takes a string as a parameter representing the name of...

    Write a function named "csv_to_list" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format "<int>,<int>,<int>,<int>,<int>" and returns a new list containing all the values in the third column as integers in the same order they appear in the input file //JAVASCRIPT //

  • Write a python function alt which takes a list of strings myList as a parameter. alt...

    Write a python function alt which takes a list of strings myList as a parameter. alt then returns two strings s1 and s2 as a tuple (s1, s2). Here s1 is the concatenation of the strings in myList in even index positions, and s2 is the concatenation of the strings in myList in odd index positions. (List starts at index 0) For example, if myList = [‘My’, ‘kingdom’, ‘for’, ‘a’ , ‘horse’], then s1 = ‘Myforhorse’ and s2 = ‘kingdoma’.

  • write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...

    write a function firstLetterWords(words) that takes as a parameter a list of strings named words and returns a dictionary with lower case letters as keys. But now associate with each key the list of the words in words that begin with that letter. For example, if the list is ['ant', 'bee', 'armadillo', 'dog', 'cat'], then your function should return the dictionary {'a': ['ant', 'armadillo'], 'b': ['bee'], 'c': ['cat'], 'd': ['dog']}.

  • Write a function that takes an array of C-strings as a parameter and the number of...

    Write a function that takes an array of C-strings as a parameter and the number of elements currently stored in the array, and returns a single C-string pointer that is the concatenation of all C-strings in the array. Note: The function must take 2 parameters and return "char *". Any other ways will cause some deduction of points.

  • More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of...

    More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of numbers as parameters, and adds all the even numbers together and returns their average using a list Example function call: print (average_evens (I-2, -3, 4, 0, 1, 2,3 Outputs: 0 Write a function match that takes two strings as a parameter and returns how many letters the strings have in common. You should treat upper and lower case letters as the same letter ('A,...

  • Write a function named "find_value" that takes a key-value store as a parameter with strings as...

    Write a function named "find_value" that takes a key-value store as a parameter with strings as keys and integers as values. The function returns a boolean representing true if the value 4 is in the input as a value, false otherwise

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT