Reputation: 323
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!
Upvotes: 18
Views: 50672
Reputation: 294
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.
Upvotes: 0
Reputation: 1
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);
Upvotes: -1
Reputation: 4510
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
Upvotes: 3
Reputation: 7590
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.
Upvotes: 33
Reputation: 78650
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).
Upvotes: 7