# How can these two scripts yield different len numbers?

624
4
04-14-2021 12:37 PM by
New Contributor II
`for i in list1:    if i in list2:        inBoth1.append(i)for i in list2:    if i in list1:        inBoth2.append(i)print(len(inBoth1))print(len(inBoth2))For my application, I am using StreetNamesInCountyBlank1 (list1) as one list and trying to see how many match items in another list, StreetNamesCountyBlank2 (list2). However, when I reverse the script like in the example above, I get a significant difference in values. `

1 Solution

Accepted Solutions by Anonymous User
Not applicable

Could be that you have duplicates in one list:

``````list1 = ['a', 'b', 'c', 'd']
list2 = ['a', 'b', 'c', 'd', 'c', 'aa', 'aaa', 'a', 'c']

inBoth1 = []
inBoth2 = []

for i in list1:
if i in list2:
inBoth1.append(i)

for i in list2:
if i in list1:
inBoth2.append(i)

print(len(inBoth1))
print(len(inBoth2))``````

gets you 4 and 7 (because of the c's).

Using set on the list you get the same number:

``````list1 = ['a', 'b', 'c', 'd']
list2 = ['a', 'b', 'c', 'd', 'c', 'aa', 'aaa', 'a', 'c']

inBoth1 = []
inBoth2 = []

for i in set(list1):
if i in list2:
inBoth1.append(i)

for i in set(list2):
if i in list1:
inBoth2.append(i)

print(len(inBoth1))
print(len(inBoth2))``````

4 and 4

4 Replies by Anonymous User
Not applicable

Could be that you have duplicates in one list:

``````list1 = ['a', 'b', 'c', 'd']
list2 = ['a', 'b', 'c', 'd', 'c', 'aa', 'aaa', 'a', 'c']

inBoth1 = []
inBoth2 = []

for i in list1:
if i in list2:
inBoth1.append(i)

for i in list2:
if i in list1:
inBoth2.append(i)

print(len(inBoth1))
print(len(inBoth2))``````

gets you 4 and 7 (because of the c's).

Using set on the list you get the same number:

``````list1 = ['a', 'b', 'c', 'd']
list2 = ['a', 'b', 'c', 'd', 'c', 'aa', 'aaa', 'a', 'c']

inBoth1 = []
inBoth2 = []

for i in set(list1):
if i in list2:
inBoth1.append(i)

for i in set(list2):
if i in list1:
inBoth2.append(i)

print(len(inBoth1))
print(len(inBoth2))``````

4 and 4 by MVP Regular Contributor

If you're trying to find the "intersection" of the two lists (values that exist in both), try comparing the lists as sets.

``set(list1).intersection(set(list2))`` New Contributor III

I would agree with JeffK, your list must have some duplications.

Another option is to create a dictionary of unique keys (your street name ?) : values (any value, lets say 1).

next looping through second feature class ( cursor) just checking if the value from that feature is in your dictionary or not (if myvalue in mydico.keys()). sth like that:

``````fields1 = ['field1','field2']
fc1 = StreetNamesInCountyBlank1
mydico = {}
# lets assume that my key will be the value from field1
with arcpy.da.SearchCursor(fc1, fields1) as recs:
for rec in recs:
if rec in mydico:
# lets count how many times the same key
mydico[rec += 1
else:
# the key is first time
mydico[rec] = 1

# now lets loop through second feature class
fc2 = StreetNamesInCountyBlank2
fields2 = ['field1','field2']
repetition_dico = {}
with arcpy.da.SearchCursor(fc2, fields2) as recs:
for rec in recs:
if rec in mydico.keys():
if rec not in repetition_dico.keys():
repetition_dico[rec = 1
else:
# you alredy know that this is repeated, lets add count only
repetition_dico[rec += 1
# print results
print( len(repetition_dico))
for key, values in repetition_dico.items():
print (key, value)`````` by MVP Esteemed Contributor

Beyond all the good feedback already provided, you never show how your "both" lists are initially created, which leaves the door open to them being different before you even started appending to them. 