Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
more list help converting
#1
I have another issue with converting a list into the formatting i need it and looking for advice. (possibly getting late in the evening and my brain has died :-( )

The current list format is as follows;

existinglist = [['A -> B', 1 ], ['C -> D', 4], ['F -> H', 6]]

I want to keep it in the same format but replace the -> as a comma, so it will have 3 indexes within each list. So i am trying to change the output to;

[['A', 'B', 1 ], ['C','D', 4], ['F','H', 6]]

My head seems to tell me the following code should work but i get an error telling me ''list' object has no attribute 'split'. Would be grateful for the solution and also if possible explanation of why my code gives this error (if possible)

My 'incorrect' effort:

        examplelist = []
        for item in existinglist:
            examplelist .append(item.split('->'))
Error message: 'list' object has no attribute 'split'

Any help and advice would be gratefully appreciated.
Reply
#2
examplelist = []
for item in existinglist:
    print(item)
If you do this you see that you get lists back.
>>> ['A -> B', 1].split(' -> ')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'

# Fix will be item[0]
>>> ['A -> B', 1][0].split(' -> ')
['A', 'B']
Most also remember to concatenate last element when append item[1].
Reply
#3
Thank you!!! That makes so much sense - I now understand where I was going wrong. Much appreciated
Reply
#4
Using list comprehension:

>>> existinglist = [['A -> B', 1 ], ['C -> D', 4], ['F -> H', 6]]                          
>>> [[*row[0].split(' -> '), row[1]] for row in existinglist]                               
[['A', 'B', 1], ['C', 'D', 4], ['F', 'H', 6]]
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
  Converting a list to dictinary tester_V 8 2,702 Jul-02-2021, 09:04 PM
Last Post: tester_V
  Converting tkinter listbox into list BigSwiggy 6 3,606 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  Converting list to variables Palves 1 1,761 Sep-18-2020, 05:43 PM
Last Post: stullis
  Trouble with converting list , dict to int values! faryad13 7 3,747 Sep-04-2020, 06:25 AM
Last Post: faryad13
  converting list of zero length to a matrix of 3*3 vp1989 2 1,924 May-20-2020, 07:46 PM
Last Post: deanhystad
  converting string object inside a list into an intiger bwdu 4 2,603 Mar-31-2020, 10:36 AM
Last Post: buran
  Converting parts of a list to int for sorting menator01 2 2,226 Nov-03-2019, 03:00 PM
Last Post: menator01
  Converting to a list and sort tantony 6 3,212 Oct-07-2019, 03:30 PM
Last Post: perfringo
  Converting List into list of tuples ARV 4 4,746 Sep-28-2019, 04:58 AM
Last Post: perfringo
  Converting List to Libray prophet11 6 3,585 Apr-22-2019, 04:49 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