using a for loop we get the values of the list:
for element in lst:
while using range gives use the indexes:
for index in range(len(list))):
so your question conflicts with what you are saying later on?
using a for loop we get the values of the list:
for element in lst:
while using range gives use the indexes:
for index in range(len(list))):
so your question conflicts with what you are saying later on?
I don’t get what you’re saying, conflicts how?
this conflicts:
with:
the for loop doesn’t gives us the indexes of the list, it gives us the values. That is why we use range()
, this gives us the indices/indexes
Don’t give up! This stuff is tough!!!
This one took me a second. I was doing a nested for loop at the beginning which was very incorrect haha
Thank you! I’m still chugging away, thanks to the help and support I’ve gotten here in the forums!
Hi, I wanted to solve the code challenge that looks for duplicats in two list and returns the index position of the matching duplicates with a while loop. However, I got stuck with the code below.
Can anyone advice how to modify that while loop?
Thx
def same_values(lst1, lst2):
new_lst = []
index = 0
while index <= (len(lst1)-1):
if lst1[index] == lst2[index]:
new_lst.append(index)
index += 1
return new_lst
you have an infinity loop. What if the first value of list one and two aren’t equal? Then index
is never increased.
That’s it!
Thanks for the fast reply!
Loops is confusing :[
def same_values(lst1, lst2):
lst3 =
for i in range(len(lst1)):
for y in range(len(lst2)):
if lst1[i] == lst2[y]:
lst3.append(i)
return lst3
this was my code, I understand that my mistake was if lst1[i] == lst2[y]: but can someone explain why that is wrong?
you have nested loops, causing too many checks.
for example, the first value of lst1 is compared to every value of lst2, which might causes too many values to be added to lst3
why do you need a nested loop? One loop is sufficient, you can use the index generated by the loop to access values in both lists.
How can I simplify the flow control code that gets the shortest list?
I want my program to be able to use lists that have different lenghts
This is my program:
def same_values(lst1, lst2):
#Find the shortest list
len1 = len(lst1)
len2 = len(lst2)
use = []
if len1 <= len2:
use = lst1
else:
use = lst2
#Check for the indents where the values are the same
same = [ i for i in range(0, len(use)) if lst1[i] == lst2[i]]
return same
print(same_values([5, 1, -10, 3, 3, 6, 7], [5, 10, -10, 3, 5, 1,]))
This is the code that enables me to use the shortest list:
#Find the shortest list
len1 = len(lst1)
len2 = len(lst2)
use = []
if len1 <= len2:
use = lst1
else:
use = lst2
*Any suggestions on how to improve my program in general would be of great help.
Hello, @gerardomtz_h. Happy Birthday!
If you could edit your post after reviewing How do I format code in my posts?, then we can see your code as you had it originally formatted. Currently, we’d be guessing at where your code is indented.
Some things you might consider (you may not be familiar with these yet, but you can look up their usage):
Thank you!
For the post formatting advice and the info!
Using variable swapping, you could eliminate the need for the extra variable, use
. Something like:
if len(lst1) > len(lst2):
lst1, lst2 = lst2, lst1
Then use the length of lst1
in your range()
inside the list comprehension.
Using enumerate()
would eliminate the need for range()
, if you want to look into how it works.
Just want to share my solution, which was a bit crazy and different from the ones i saw here:
def same_values(lst1,lst2):
newlst=[]
for x,y in zip(lst1,lst2):
if x==y:
newlst.append(lst1.index(x))
return newlst
#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))
Hey,
I know you replied on February but still, you might help me.
I was having the same issue and was trying the nested loop and I knew I needed the indexation so I was trying to put the "range(0,len(lst1)) inside of the nested. Might sound stupid but what did you think to understand that you needed to do the opposite, get the indexation, see if the numbers in the list are equal and add the indexation to the new list?
Thank you.
def same_values(lst1, lst2):
new_list =[]
for index in range(len(lst1)):
#for index in range(len(lst2)):
if(lst1[index]==lst2[index]):
new_list.append(index)
return new_list
#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))
I stumbled upon the correct answer by mistake after I commented out a line. My question is how does the program know to compare the indexes with lst1 with the indexes with lst2 if we don’t explicitly program that. Am I overthinking this one or does it simply work because we use len(lst)?
It works because we’re assuming both lists have the same length, therefore corresponding values at each index. The range is just a sequence of integers from 0 to length - 1.
so technically this method only works for list of the same length?