Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strip()
#1
Hi,
Var1 is a list and I'd like to strip characters from each item in the list and save the result as a new list called Var2. I can't find a way to do it. The code below seemed to be the logical solution but it doesn't work. What am I missing here? Thanks.

for item in Var1:
    Var2 = item.strip('1234 :')
    print(Var2)
Output:
['1234 :A'] ['1234 :B'] ['1234 :C']
Reply
#2
post sample of Var1, also note the inconsistency in your code - you use Var2 and var2
the output suggest you have nested list of lists.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Feb-16-2021, 10:50 AM)buran Wrote: post sample of Var1, also note the inconsistency in your code - you use Var2 and var2
the output suggest you have nested list of lists.

Thanks, buran. Var1 is the same as the output: ['1234 :A'], ['1234 :B'], ['1234 :C']

The 'for loop' I wrote doesn't change Var1 in any way. I want Var2 to be: ['A'], ['B'], ['C'].

Thanks for pointing out the inconsistency Var2 vs var2.
Reply
#4
(Feb-16-2021, 11:13 AM)pprod Wrote: Var1 is the same as the output: ['1234 :A'], ['1234 :B'], ['1234 :C']

If this is really the case, then you have tuple of lists (note having brackets is not always mandatory to define tuple.
can you print(repr(var1)) just before the loop and post the result, so that we know what you work with.

var1 =['1234 :A'], ['1234 :B'], ['1234 :C']
print(type(var1))

var2 = tuple([item[0].split(':')[-1]] for item in var1)
print(var2)
var2 = tuple([item[0][6:]] for item in var1)
print(var2)
strip() is a bit tricky and you should be careful with. - it will strip from both ends and will strip chars separetly, e.g. '123432 1 :' will also be stripped
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Feb-16-2021, 11:58 AM)buran Wrote:
(Feb-16-2021, 11:13 AM)pprod Wrote: Var1 is the same as the output: ['1234 :A'], ['1234 :B'], ['1234 :C']

If this is really the case, then you have tuple of lists (note having brackets is not always mandatory to define tuple.
can you print(repr(var1)) just before the loop and post the result, so that we know what you work with.

var1 =['1234 :A'], ['1234 :B'], ['1234 :C']
print(type(var1))

var2 = tuple([item[0].split(':')[-1]] for item in var1)
print(var2)
var2 = tuple([item[0][6:]] for item in var1)
print(var2)
strip() is a bit tricky and you should be careful with. - it will strip from both ends and will strip chars separetly, e.g. '123432 1 :' will also be stripped


Thanks, buran. If I print(repr(Var1)) I get Var1 again:['1234 :A'], ['1234 :B'], ['1234 :C']. I'm cosidering running a RegEx on Var1 to capture the text I want and save as Var2.
Reply
#6
(Feb-16-2021, 12:18 PM)pprod Wrote: print(repr(Var1)) I get Var1 again:['1234 :A'], ['1234 :B'], ['1234 :C'].
not possible, in the best case - you get "['1234 :A'], ['1234 :B'], ['1234 :C']" - note the double quotes, in which case you have a str. But in this case the output in your post1 will not make sense unless if you have new lines
what is the output from
print(type(Var1))
How you don't know what type Var1 is. Ideally, post minimal reproducible example that we can copy in IDE and run it. Var1 including.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Feb-16-2021, 10:43 AM)pprod Wrote: Var1 is a list and I'd like to strip characters from each item in the list and save the result as a new list called Var2.

Unless I'm missing something, this basically sounds like a use for a list comprehension or map.
Reply
#8
(Feb-16-2021, 12:29 PM)buran Wrote:
(Feb-16-2021, 12:18 PM)pprod Wrote: print(repr(Var1)) I get Var1 again:['1234 :A'], ['1234 :B'], ['1234 :C'].
not possible, in the best case - you get "['1234 :A'], ['1234 :B'], ['1234 :C']" - note the double quotes, in which case you have a str. But in this case the output in your post1 will make sense unless if you have new lines
what is the output from
print(type(Var1))
How you don't know what type Var1 is. Ideally, post minimal reproducible example that we can copy in IDE and run it. Var1 including.

print(type(Var1)) returns <class 'list'>. Sorry, I can't include an example that can be reproduced as it involves opening a confidential file and running a RegEx on every page of that file. Var1 is a list that contains the matches for every page. I'm really sorry, I'm a beginner and I can't explain myself in the best possible way sometimes. Thanks for your time.
Reply
#9
OK, list it is
var1 = [['1234 :A'], ['1234 :B'], ['1234 :C']]
print(type(var1))
 
var2 = [[item[0].split(':')[-1]] for item in var1]
print(var2)
var2 = [[item[0][6:]] for item in var1]
print(var2)

# or one by one in a loop:
for item in var1:
   prefix, var2 = item[0].split(':')
   print([var2]) # to put var2 inside a list, because I understand that is waht you want
Output:
<class 'list'> [['A'], ['B'], ['C']] [['A'], ['B'], ['C']] ['A'] ['B'] ['C']
pprod likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,999 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,211 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Can't strip extra characters from Data Canflyguy 7 1,878 Jan-10-2022, 02:16 PM
Last Post: Canflyguy
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,772 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  split strip issues 'NonType' casacafe 8 3,907 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,603 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,355 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 2,903 Apr-15-2019, 03:14 AM
Last Post: ineuw
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 4,481 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE
  Strip does not work when applied on a string read from a text file. susmis666 1 2,398 Dec-27-2018, 07:07 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020