How to delete the very last character from every string in a list of strings

Question:

I have the strings ‘80010’, ‘80030’, ‘80050’ in a list, as in

test = ['80010','80030','80050']

How can I delete the very last character (in this case the very last digit of each string which is a 0), so that I can end up with another list containing only the first four digits/characters from each string? So end up with something like

newtest = ['8001', '8003', '8005']

I am very new to Python but I have tried with if-else statements, appending, using indexing [:-1], etc. but nothing seems to work unless I end up deleting all my other zeros. Thank you so much!

Asked By: spartmar

||

Answers:

test = ["80010","80030","80050"]
newtest = [x[:-1] for x in test]

New test will contain the result ["8001","8003","8005"].

[x[:-1] for x in test] creates a new list (using list comprehension) by looping over each item in test and putting a modified version into newtest. The x[:-1] means to take everything in the string value x up to but not including the last element.

Answered By: Matthew

You are not so far off. Using the slice notation [:-1] is the right approach. Just combine it with a list comprehension:

>>> test = ['80010','80030','80050']
>>> [x[:-1] for x in test]
['8001', '8003', '8005']

somestring[:-1] gives you everything from the character at position 0 (inclusive) to the last character (exclusive).

Answered By: timgeb

Just to show a slightly different solution than comprehension, Given that other answers already explained slicing, I just go through at the method.

With the map function.

test = ['80010','80030','80050']
print map(lambda x: x[:-1],test)
# ['8001', '8003', '8005']

For more information about this solution, please read the brief explanation I did in another similar question.

Convert a list into a sequence of string triples

Answered By: Rockybilly

In python @Matthew solution is perfect. But if indeed you are a beginer in coding in general, I must recommend this, less elegant for sure but the only way in many other scenario :

#variables declaration
test = ['80010','80030','80050'] 
length = len(test)                 # for reading and writing sakes, len(A): length of A
newtest = [None] * length          # newtest = [none, none, none], go look up empty array creation
strLen = 0                         # temporary storage

#adding in newtest every element of test but spliced
for i in range(0, lenght):         # for loop
    str = test[i]                  # get n th element of test
    strLen = len (str)             # for reading sake, the lenght of string that will be spliced
    newtest[i] = str[0:strLen - 1] # n th element of newtest is the spliced n th element from test

#show the results
print (newtest)                    # ['8001','8003','8005']

ps : this scripts, albeit not being the best, works in python ! Good luck to any programmer newcommer.

Answered By: NRagot

I had a similar problem and here is the solution.

List<String> timeInDays = new ArrayList<>();
timeInDays.add(2d);
timeInDays.add(3d);
timeInDays.add(4d);

I need to remove last letter in every string in-order to compare them. Below solution worked for me.

List<String> trimmedList = new ArrayList<>;
for(int i=0;i<timeInDays.size();i++)
{
String trimmedString = timeInDays.get(i).substring(0,name.length()-1);
trimmedList=add(trimmedString );
}

System.out.println("List after trimming every string is "+trimmedList);
Answered By: Kanthu

new_list = [x[:-1] for x in previous_list]

Answered By: Ash
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.