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
#1
I have a list which is in the following format:

list = ['A -> B', 'C -> D', 'E -> F']

I am trying to find a way to produce a list of single entries in the following format:

['A','B','C','D','E','F']

When I incorrectly attempt to split by using;

        newList = []
        for item in list:
            newList.append(item.split('->'))
        print(newList)
This incorrectly produces:

[['A','B], ['C','D'], ['E','F']]

Any help and advice would be greatly appreciated.
Reply
#2
Never use list as a name. You overwrite built-in datatype list.

You should use 'extend' instead of 'append':

>>> lst =  ['A -> B', 'C -> D', 'E -> F']
>>> new_list = []
>>> for item in lst:
...     new_list.extend(item.split(' -> '))
... 
>>> new_list
['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
#3
Thank you that was exactly what I was looking for and gives me the results I require. 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?
Reply
#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


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,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to split the input taken from user into a single character? mHosseinDS86 3 1,166 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,295 Jul-11-2022, 08:52 AM
Last Post: japo85
  How to get unique entries in a list and the count of occurrence james2009 5 2,962 May-08-2022, 04:34 AM
Last Post: ndc85430
  Split single column to multiple columns SriRajesh 1 1,319 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  How to efficiently average same entries of lists in a list xquad 5 2,114 Dec-17-2021, 04:44 PM
Last Post: xquad
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,344 Apr-11-2021, 11:03 PM
Last Post: lastyle
  convert List with dictionaries to a single dictionary iamaghost 3 2,851 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,316 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Undo interation to make a single list? DustinKlent 2 2,166 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