3 posts were merged into an existing topic: What is an index?

# FAQ: Code Challenge: Loops - Same Values

I had trouble with this initially because I indented the return with the first for loop.

it returned [0, 2, 3, 0, 2, 3, 0, 2, 3, 0, 2, 3, 0, 2, 3] because it repeated the command from the second for loop for the 5 index positions in lst1, correct?

is this the best way of doing it?

if not how can i improve it?

this returns the indices which match but before all that it checks which list is smaller and uses that as the range

```
def same_values(lst1,lst2):
index = []
length = range(len(lst1))
length2 = range(len(lst2))
if len(lst1) <= len(lst2):
for i in length:
if lst1[i] == lst2[i]:
index.append(i)
return index
else:
for i in length2:
if lst1[i] == lst2[i]:
index.append(i)
return index
```

`zip`

ping the two lists resolves the uneven length concern by truncating the longer of the two lists. Values are paired up by their indices. 0:0, 1:1, 2:2, &c. Mind, the instructions do state *two lists of numbers of equal size*.

With a zipped list we only iterate a single object. Using `enumerate`

we can track the index.

```
for i, x in enumerate(zip(lst1, lst2)):
```

`x`

is a tuple, so we can unpack it…

```
a, b = x
```

```
def same_values(lst1, lst2):
y = []
for i, x in enumerate(zip(lst1, lst2)):
a, b = x
if a == b:
y.append(i)
return y
```

The more naive approach is to keep track of the index with a **count** variable.

```
count = 0
y = []
for x in zip(lst1, lst2):
a, b = x
if a == b:
y.append{count)
count += 1
return y
```

We will most certainly come up with other naive approaches if one sticks with the problem awhile. Play it out and see how many you can come up with.

here’s my solution

```
def same_values(lst1, lst2):
same = []
i = 0
if len(lst1) == len(lst2):
for num in lst1:
if lst1[i] == lst2[i]:
same.append(i)
i = i + 1
return same
```

Sorry - didn’t realize that’s a faux pas. Just wanted to document one method of completion, since I like looking at how others solve.

I can obscure the solutions.

Yes, that would be good idea. Please do so.

Hello i believe i have solved the code chalanges (loops) same values. But i can’t pass the subject

here is my code

```
#Write your function here
def same_val(lst1, lst2, res=[]) :
fa = lst1[0]
fb = lst2[0]
ra = lst1[1:]
rb = lst2[1:]
r = []
if fa == fb :
r.append(fa)
if ra == [] :
return res
return same_val(ra,rb, res+r)
def same_values(lst1, lst2) :
res = same_val(lst1,lst2)
result = []
for n in res :
result.append(lst1.index(n))
return result
#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))
```

I’m using helper function same_val to recursively get the same values and then get the indicies in the same_values function. is this a bug or what? thanks

Hi all,

I figured out the for-loop solution but was trying to find and alternative using list comprehension… and failed. What’s wrong with this / what is the way? (error was list out of range). My sense is that the index object is looking for “i” rather than what I intend which is the first element of lst1 if it is the same as the first element of lst2.

```
svi = [lst1.index(i) for i in lst1 if lst1[i] == list2[i]]
```

Thanks for any help.

your variable `i`

contains value from the list, so trying to access by index (`lst1[i]`

) will throw an index error when there is a value in the list which is higher then the highest index (which is quite common)

i wouldn’t use the `.index()`

method, the index method always gives the first match, which is problematic if one value occurs multiple times in a list

Thanks. Is there a way to use list comprehension to solve?

yes, there is. You could use `range()`

to get the indexes/indices. That should help

How to obscure solutions?

Use `[details="name"]`

to create a show/hide block. Close it with `[/details]`

.

Use `[spoiler]`

to convert code to a PNG that cannot be copied. Close it with `[/spoiler]`

## is_odd

```
def is_odd(x):
return x % 2 and True
```

[details="is_odd"] [spoiler] ``` def is_odd(x): return x % 2 and True ``` [/spoiler] [/details]

Please critique my solution. After a lot of tinkering, I managed to get it to work in a list comprehension:

```
def same_values(lst1, lst2):
return [lst1.index(x) for x, y in zip(lst1, lst2) if x == y]
```