0

Why this is iteration is not working ? while the same works if words is replaced by range(len(words)) in the for loop

n = ["Michael", "Lieberman"] def join_strings(words): result = "" for i in words: result += words[i] return result print join_strings(n) 
2
  • In you add print(i) in the loop, you will see that i is not what you think it is. Commented Jan 18, 2017 at 5:56
  • @tarashypka thanks for the clarification. Can u please suggest an alternate to get the answer without using range() function Commented Jan 18, 2017 at 6:11

3 Answers 3

2

Your code.

n = ["Michael", "Lieberman"] def join_strings(words): # words = ["Michael", "Lieberman"] result = "" for i in words: # i = 'Michael' for the first iteration. # if you do range(len(words)) then i=0 and words[0] is valid then result += words[i] #<--Error! i='Michael' and you can't do words['Michael'] return result print join_strings(n) 

Pythonic way of joining strings in the list:

print ''.join(n) #Outputs --> MichaelLieberman 
Sign up to request clarification or add additional context in comments.

1 Comment

it's not only a pythonic problem: using string concatenation is under-performant, and should be avoided at all costs.
0

i is a string value not index. You can use enumerate function:

for index, i in enumerate(words): result += words[index] 

or you can just use the word itself:

for i in words: result += i 

Performance:

In [14]: timeit.timeit('string_cat', '''def string_cat(): ...: rg = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', ...: 't', 'u', 'v', 'w', 'x', 'y', 'z'] ...: st = '' ...: for x in rg: st += x ...: ''', number=1000000) Out[14]: 0.02700495719909668 In [15]: timeit.timeit('string_cat', '''def string_cat(): ...: rg = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', ...: 't', 'u', 'v', 'w', 'x', 'y', 'z'] ...: st = ''.join(rg) ...: ''', number=1000000) Out[15]: 0.026237964630126953 

3 Comments

first solution is unpythonic like hell, and both are highly inefficient. don't use that code.
@Jean-FrançoisFabre: surprisingly, after testing, string concatenation is not necessarily worse than join; that said, I definitely prefer join myself.
you have to perform real test, with big strings. reallocation/memory copy is the actual problem. I've been there myself. and in-place doesn't help a bit because strings are immutable.
0

if you r using for loop like :

for i in words: 

then do like this :

result += i 

because here we are taking values from list directly without mentioning any range or it's size.

if u r using for loop like :

for i in range(len(words)): 

then do like this :

result += words[i] 

and the output will be same from both the cases :

MichaelLieberman 

for better clarification about for loop refer this : https://www.learnpython.org/en/Loops

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.