Question

Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus.

Your program should contain two functions:

(1) remove_first(list): This function takes in a list as a parameter, and returns a new list of the same items, but with first character removed. No print in this function. Hint: You may use List Comprehension in this function. (Submit for 6 points)

Example:

Call: remove_first(['#apple', '#banana', '#cherry'])  ----> Return:  ['apple', 'banana', 'cherry']

(2) "__main__": In the main, you do the following: (Submit for 4 points)

a. When you start the program, print "Remove first character in list of strings"

b. Use the list in this program, hard coded. You may change for more testing.

c. printing original list.

d. Call the function remove_first, then print the new list after removing first character.

Here are example runs of the program:

Example 1:

Remove first character in list of strings
The original list:  ['#apple', '#banana', '#cherry']
The list after removing first characters: ['apple', 'banana', 'cherry']

Example 2:

Remove first character in list of strings
The original list:  ['$25.0', '$13.75', '$10.95', '$4.99']
The list after removing first characters: ['25.0', '13.75', '10.95', '4.99']
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def remove_first(list):
    result = []
    for s in list:
        result.append(s[1:])
    return result


if __name__ == '__main__':
    print("Remove first character in list of strings")
    lst = ['#apple', '#banana', '#cherry']
    print("The original list:", lst)
    print("The list after removing first characters:", remove_first(lst))
Add a comment
Know the answer?
Add Answer to:
Write a Python program (remove_first_char.py) that removes the first character from each item in a list...
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
  • In Python 1 2 Write a function that removes all occurrences of a given letter from...

    In Python 1 2 Write a function that removes all occurrences of a given letter from a string: test (remove_letter ("a", "apple") "pple") test (remove_letter ("a", "banana") "bnn") test (remove_letter (2", "banana") "banana") test (remove_letter ("i", "Mississippi") "Msssspp") test (remove letter (",) "") test (remove_letter("b","0"> MO) 4 5 ### Run the following lines. # If you get "Correct" you # did it right. if remove_letter("a", "banana") == "nn": print("Correct!") else: print("Incorrect :(")

  • Write a Python program to create userids: You work for a small company that keeps the...

    Write a Python program to create userids: You work for a small company that keeps the following information about its clients: • first name • last name • a user code assigned by your company. The information is stored in a file clients.txt with the information for each client on one line (last name first), with commas between the parts. In the clients.txt file is: Jones, Sally,00345 Lin,Nenya,00548 Fule,A,00000 Your job is to create a program assign usernames for a...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq)....

    In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter words to the function...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • Write a program in Python that finds the index of an item in a list. For...

    Write a program in Python that finds the index of an item in a list. For example: You create a of list of colors You find the index of the color "red" Print the list and the index of "red" Your program should work for every item in your list, and it must have at least 3 items in the list. It must also work if you search for an item and it does not exist in the list. You...

  • 5.19 LAB: Contains the character Write a program that reads an integer, a list of words,...

    5.19 LAB: Contains the character Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list will always contain less than 20 words. Each word will always contain less than 10 characters...

  • Provide Python code that does the following: Create the following list in your program: lst1 =...

    Provide Python code that does the following: Create the following list in your program: lst1 = ["apple", "banana", "pear", "grapefruit", "pineapple", "grape", "guava", "plum", "peach"] Write code that then does the following: Print the last item in the list. Print the first item in the list. Print all but the last three items in the list. Print the first, third, fifth etc. items of the list. Print the second, fourth, sixth etc. items of the list. Parts A- E should...

  • Write in c++ please Imagine you are writing a program to manage a shopping list. Each...

    Write in c++ please Imagine you are writing a program to manage a shopping list. Each shopping list item is represented by a string stored in a container. Your design requires a print function that prints out the contents of the shopping list Using a vector to hold the shopping list items, write a print function to print out the contents of a vector of strings. Test your print function with a main program that does the following: 1. Create...

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