When you do for i in row, i takes the values in row, i.e. '1', then '2', then '3'.
Those are strings, which are not valid as a list index. A list index should be an integer. When doing range(len(row)) you loop on integers up to the size of the list.
By the way, a better approach is:
for elt_id, elt in enumerate(list):
# do stuff
Answer from Mathieu on Stack OverflowIterating over a list in python using for-loop - Stack Overflow
Is it possible to iterate over a list of lists in a function?
python - How to loop over a list of lists - Stack Overflow
Iterate through a list and perform an action for each item in the list
Videos
When you do for i in row, i takes the values in row, i.e. '1', then '2', then '3'.
Those are strings, which are not valid as a list index. A list index should be an integer. When doing range(len(row)) you loop on integers up to the size of the list.
By the way, a better approach is:
for elt_id, elt in enumerate(list):
# do stuff
Given row = ['1', '2', '3'], each item inside row is a string, thus it can be easily printed:
for i in row:
print(i)
However, in your second example you are trying to access row using a string. You need an integer to access values inside a list, and row is a list! (of strings, but a list still).
for i in row:
# row[i] = int(row[i]) -> this will throw an error
row[int(i)] = int(row[int(i)]) # this will throw an error
I have this exercise I am tring online which take a bunch of lists and zip them together. first and then returns a list. For example, the input func([1,2,3], [20,30,40], ['a', 'b', 'c']) should return [1, 20, 'a', 2, 30, 'b', 3, 40, 'c']
However I do not know how to iterate over the list. I don't even know if it's possible. I tried
def func(*args):
for n in args:
ans = list(zip(n))
return ans
But I just get : [(1,), (2,), (3,)]
x[0] returns the item at index 0 of the list x, so by appending to a new list with x[0] in your for loop, you are appending the value within the sub-list rather than the sub-list itself, thereby achieving the effect you want, flattening the list.
Could you please tell what does "x[0]" do in this for loop; I thought this index would mean only taking the first element in a list.
list1 = [[1],[2],[3]] is not simply a list but a list of lists. Every element of list1 is a list โ albeit with only one sub-element. So you are effectively iterating over every list within your list list1, picking the first element at position 0 (which is all there is) and appending it to your new list list2.
Hi, so I am quite new to python however have an issue I am struggling to come up with convincing solution to.
I have a list of strings - around 27 items - and have also created a function that calculates the Jaro distance between the two String variables, this works as far as I can tell however I now need to form a loop that will allow me to apply this function for each item in the list against each other item in the list.
So far I have been using the following for loop:
for i in description_list:
print(jaro_distance(str(description_list[1]),str(description_list[count+1])))
count = count + 1print(jaro_distance(str(description_list[1]), str(description_list[count+1]))) count = count + 1
but this only works for comparing the fist item in the list vs the following items. So my question is this, how can I create a loop that iterates each item against every other item in the list?