0

Can someone explain me why after the for loop the list res is ['m']?

string = 'spam'
for x in string:
 res =[]
 res.extend(x)
print(res)

I expected the output to be res = ['s', 'p', 'a', 'm']

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Oct 19, 2019 at 17:42
2
  • str is weird, due to the lack of a dedicated char type in Python. A str value acts like a container of 1-character str values. Commented Oct 19, 2019 at 18:43
  • 's' == 'spam'[0] == 'spam'[0][0] == 'spam'[0][0][0] == .... , no matter how many [0] you use. Commented Oct 19, 2019 at 18:49

5 Answers 5

2

You are replacing the list object each step of your loop. The statement res = [] creates a new, empty list object, then adds a single letter to that list.

Without the loop, this is what you are doing:

>>> x = 's'
>>> res = []
>>> res.extend(x)
>>> res
['s']
>>> x = 'p'
>>> res = []
>>> res.extend(x)
['p']
>>> x = 'a'
>>> res = []
>>> res.extend(x)
>>> res
['a']
>>> res = []
>>> x = 'm'
>>> res.extend(x)
>>> res
['m']

Create the list outside of the loop, once:

string = 'spam'
res = []
for x in string:
 res.extend(x)
print(res)

Now you don't keep replacing the list object with a new one each iteration of the for loop.

Again, removing the loop and doing the steps manually, now we have:

>>> res = []
>>> x = 's'
>>> res.extend(x)
>>> res
['s']
>>> x = 'p'
>>> res.extend(x)
>>> res
['s', 'p']
>>> x = 'a'
>>> res.extend(x)
>>> res
['s', 'p', 'a']
>>> x = 'm'
>>> res.extend(x)
>>> res
['s', 'p', 'a', 'm']

Not that you should be using res.extend() here; it only works because individual letters in string assigned to x are each also strings and even single-letter strings are still sequences. What you are really doing with res.extend(x) is the equivalent of for element in x: res.append(element), but x will always have just one element.

So this would work too:

string = 'spam'
res = []
for x in string:
 res.append(x)
print(res)

or just extend res with the whole string value:

string = 'spam'
res = []
res.extend(string)
print(res)

or, if you just wanted a list of all the characters of a string, just use the list() function:

string = 'spam'
res = list(string)
print(res)

list() does exactly what you wanted to do with your loop: create an empty list, loop over the input, and add each element to the new list, which is then returned:

>>> string = 'spam'
>>> list(string)
['s', 'p', 'a', 'm']
answered Oct 19, 2019 at 17:44
Sign up to request clarification or add additional context in comments.

Comments

0

You are resetting res every time inside the loop. You need to use this-

string = ‘spam’
res =[]
for x in string: 
 res.extend(x)
print(res)
answered Oct 19, 2019 at 17:49

Comments

0

You'll never get that output because for every iteration of the loop, you are setting res = [] and therefore only the last iteration will work by extending the blank list with 'm'.

The fixed code looks like this:

string = 'spam'
res = []
for x in string:
 res.extend(x)
print(res)

Another note is that you probably should use .append in this case. .extend is for appending an entire iterable but since you are only adding one element at a time it isn't necessary. Check here for a good explanation.

Also a last note here is that you'll want to be careful with editing python code outside of plain text or code editors. You're using some leading and trailing apostrophes ‘’ instead of regular '' which will cause you trouble at some point.

answered Oct 19, 2019 at 17:44

Comments

0

You are always re-initializing the res list in the for-loop, that is why in the last iteration of the loop the list is initialized to [] an empty list and the last letter is added to it.

string = 'spam'
res =[]
for x in string:
 res.extend(x)
print(res)

or to make it simple, use the list builtin which takes an iterable like a string and converts it into an list object:

>>> list('spam')
['s', 'p', 'a', 'm']
answered Oct 19, 2019 at 17:46

Comments

0

I think this is simplest way:

string = 'spam'
res = list(string)
answered Oct 19, 2019 at 18:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.