1

I just started learning to program with Python for private interest and I am following a on-line bootcamp. I am supposed to solve some exercises and I started very soon to find the firse difficulties. So, I have this list:

chaos =["old price: 40", "new price: 21", "old price: 29", "old price: 50", "new price: 101"]

and I would like to insert the list "chaos" in the variable l in order to split it on the colon, so I typed:

l = chaos.split(":")

print(l)

and, as follows, the error message:

AttributeError                            Traceback (most recent call last)
<ipython-input-12-31400247da2e> in <module>
      1 chaos =["old price: 40", "new price: 21", "old price: 29", "old price: 50", "new price: 101"]
      2 
----> 3 l = chaos.split(":")
      4 
      5 print(l)

Error:

AttributeError: 'list' object has no attribute 'split'

I wrote and rewrote it after looking other examples and tutorials but I don´t really understand what I am doing wrong. Can somone help me?

AMC
  • 2,642
  • 7
  • 13
  • 35
Franz Biberkopf
  • 191
  • 1
  • 11
  • You have to call `split` on each element of the list, not the list itself. `split` is a method of a string. – Matthias Mar 26 '20 at 17:05
  • 1
    what is your expected output ? – kederrac Mar 26 '20 at 17:12
  • Does this answer your question? [Attribute Error: 'list' object has no attribute 'split'](https://stackoverflow.com/questions/30042334/attribute-error-list-object-has-no-attribute-split) – AMC Mar 26 '20 at 20:41

3 Answers3

1

you have to split each element from the list chaos, you could use a for loop:

l = []

for c in chaos:
    l.extend(c.split(':'))
l

output:

['old price',
 ' 40',
 'new price',
 ' 21',
 'old price',
 ' 29',
 'old price',
 ' 50',
 'new price',
 ' 101']

you can read more about str.split here

kederrac
  • 16,819
  • 6
  • 32
  • 55
1

kederrac's answer is correct. Alternatively, you can use a list comprehension as such:

l = [value for entry in chaos for value in entry.split(':')]

Which gives the output:

['old price', ' 40', 'new price', ' 21', 'old price', ' 29', 'old price', ' 50', 'new price', ' 101']

Looking at the actual cause of the error, the message explains exactly what has happened. A list object, in this case chaos, does not have access to the split method. That is a reserved method for strings

Furthermore, if you want to keep the list of elements from the split, you can simplify the comprehension to just the following, as suggested by boechat107.

l = [entry.split(':') for entry in chaos]

which would result in:

[['old price', ' 40'], ['new price', ' 21'], ['old price', ' 29'], ['old price', ' 50'], ['new price', ' 101']]
afterburner
  • 2,552
  • 13
  • 21
0

split() is an exclusive string´s method. Test the command type(chaos) and you will check it out.

To solve your problem, as said in some answers and comments above, you need to iterate through the elements of the chaos list.

You can use the list compreension [element.split(":") for element in chao] or a for loop instead:

l = []
for element in chaos:
    l.append(element.split(":"))
Henrique Branco
  • 1,778
  • 1
  • 13
  • 40