Python Forum
List help to split into single entries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List help to split into single entries
#4
(Nov-24-2019, 04:31 PM)paul41 Wrote: In my data set I seem to have some whitespace in some of the items. Do you know how i can remove this within the same for loop?

If the whitespaces are around arrow then code above takes care of that (it breaks at ' -> '). If there are additional whitespaces (at the beginning or end) then they could be stripped:

>>> lst =  ['A -> B ', ' C -> D', ' E -> F ']                          # note spaces at start and end                                             
>>> new = []                                                                               
>>> for item in lst: 
...    new.extend(chunk.strip() for chunk in item.split(' -> ')) 
...                                                                                        
>>> new                                                                                    
['A', 'B', 'C', 'D', 'E', 'F']
>>> [chunk.strip() for item in lst for chunk in item.split(' -> ')]    # same result with list comprehension                      
['A', 'B', 'C', 'D', 'E', 'F']                                      
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
List help to split into single entries - by paul41 - Nov-24-2019, 03:37 PM
RE: List help to split into single entries - by perfringo - Nov-25-2019, 08:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,544 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to split the input taken from user into a single character? mHosseinDS86 3 1,204 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,324 Jul-11-2022, 08:52 AM
Last Post: japo85
  How to get unique entries in a list and the count of occurrence james2009 5 3,015 May-08-2022, 04:34 AM
Last Post: ndc85430
  Split single column to multiple columns SriRajesh 1 1,345 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  How to efficiently average same entries of lists in a list xquad 5 2,168 Dec-17-2021, 04:44 PM
Last Post: xquad
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,434 Apr-11-2021, 11:03 PM
Last Post: lastyle
  convert List with dictionaries to a single dictionary iamaghost 3 2,886 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,363 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Undo interation to make a single list? DustinKlent 2 2,202 Nov-29-2020, 03:41 AM
Last Post: DustinKlent

Forum Jump:

User Panel Messages

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