Using strings in lists in functions


#1




There are two methods for looping through a list, using "for item in list" and "for item in range(len(list))". However, in this exercise it works only with the second method, and when I try to use the first it shows "TypeError: list indices must be integers, not str". What's the problem?



Replace this line with your code.


#2

When we iterate over some sequence data type,our iterator takes value of items of that sequence, rather the index.

Like List, Tuple, String etc are sequence type data structures.

If you want to use index then you have to loop over length of sequence like this using range() method.

for i in range(0,len(sequence)):
    #yourcode

but when you do this..

for i in sequence:
    #yourcode

The value of each items get saved to i at each iteration.


#3

I think I understood that, but why should we specify the range in "range" form 0 to len(sequence)? I thought it should be 0 by default. Why does it stuck when only len(sequence) is specified?


#4

Why does it stuck when only len(sequence) is specified?

It worked for me, I just tested (Your code at github)

I was explaining why your second code in snap did not worked.
Yeah You can use that too as range() by default starts from 0 , I was just specific while putting 0. :slight_smile:


#5

Ok, thank you!

Greentreemee codecademy@discoursemail.com於 2017年6月24日 週六,13:20寫道:


#6

Thank you for your previous explanation, I've just come up with another question:
When iterator gets a string value in the "for item in list" method, why it demands the value to be an integer in order to add those? Can't string also be added aka concetenated?


#7

The data structure like strings, lists , tuples are 0 - indexed and ordered.
We can access their items (items can be any object not just string) using the index.
when we loop over length of some sequence, we use index to access the individual items ,

in second case when we directly loop over it, then it takes items value at each iteration.
Check this thread,


#8

Almost got it. So in the exercise above if the list contained, for instance, integers instead of strings, the second case ( the "for i in l" method) would work just as well, right?


#9

It will work but as you can see we are concatenating ,and concatenation is only defined for same data types so we will need to cast it using some constructor like str().

lets say..

a_versatile_list = ["String" , 12, True, (1,2,3),[1,2]]

if you loop over it..

for i in a_versatile_list:
    print i

It will print each item one by one..

String
12
True
(1,2,3)
[1,2]

#10

Ok thank you very much! I think I got it


#11

while you go through exercises of CC ,also read some side tutorials, there are tons of them on the net,!


#12

If you want to do it the way using a for loop without the range function, you need to change results = results + words[i] to what I have below. Combine this with what others have explained above.

n = ["Michael", "Lieberman"]
def join_strings(words):
    results = ""
    for i in words:
        results = results + i
    return results


print join_strings(n)

This will return MichaelLieberman


#13

thank you for your reply!


#14

Any idea what is going wrong here?


#15

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for i in words:
result = result + i
return result

print join_strings(n)

Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned 'x' instead of 'xyza'


#16

appears that indentation of return statement is out of structure so we have this result.
return statement simply halts a function when function reaches it.
we need to return final concatenated version of string after we add all of the strings.


#17

So my return function is being included in the loop instead of after the loop?


#18

Thanks... problem solved!


#19

return is not a function, its a keyword that used to make return statements.
It resides inside then function and function use it to hand over some data to any place where function gets called.
I think your code looks like this now(I'm just guessing), You can use three ticks or </> to format your code, its hard to look how your code is structured, I guess its like this?

def join_strings(words):
    result = ""
    for i in words:
        result = result + i
        return result

print join_strings(n)

#20

It was. I thought the picture (my initial post) made the text difficult to read, so I did a copy/paste for the first reply.
After I shift-tabbed the return line, the function worked as intended.