Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

Fix quotes, fancy curly quotes are not really Python syntax.
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.5k

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

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

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

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’]

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']

edited title
Link

Python: append/ extend method

added 16 characters in body
Source Link
MyNameIsCaleb
  • 4.5k
  • 1
  • 17
  • 32

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

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

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

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

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’]

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’]

Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /