1

I have two lists of different lengths.

list_a = [32.959723, 32.969722999999995, 32.97972299999999, 32.98972299999999, 32.99972299999999, 33.00972299999999, 33.019722999999985, 33.02972299999998]

list_b = [35.808097, 35.818096999999995, 35.82809699999999, 35.83809699999999, 35.84809699999999, 35.85809699999999]

I want to create a function that compares the lengths of the two lists and remove the excess values from the bigger list.

I'm guessing it's something like

if len(list_a) != len(list_b):
   #code to match them.
  • Do you mean the values that are the same in both lists? This is a great time to use a set. You can something like ``` set_a = set(list_a) set_b = set(list_b) leftovers = set_a.difference(set_b) ``` – bjk116 Dec 23 '19 at 18:27
  • This is a possible duplicate of https://stackoverflow.com/questions/4211209/remove-all-the-elements-that-occur-in-one-list-from-another – Rahul P Dec 23 '19 at 18:32
  • Please be more specific. Do you want to remove values at random? – AMC Dec 23 '19 at 18:36

3 Answers3

2

You just need to find the minimum length and slice both list at this size (the biggest will be sliced, and the smallest will keep all)

min_length = min(len(list_a), len(list_b))
list_a = list_a[:min_length]
list_b = list_b[:min_length]


# same but shorten
list_a, list_b = list_a[:min(len(list_a), len(list_b))], list_b[:min(len(list_a), len(list_b))]

If you care about "not applying computation when unnecessary" you may use a condition :

if len(list_a) < len(list_b):
    list_b = list_b[:len(list_a)]
elif len(list_a) > len(list_b):
    list_a = list_a[:len(list_b)]
azro
  • 53,056
  • 7
  • 34
  • 70
2

You could use slicing feature by comparing the lengths.

if len(list_a) < len(list_b):
   list_b = list_b[: len(list_a)]
elif len(list_a) > len(list_b):
   list_a = list_a[: len(list_b)]

Output

list_a = [32.959723, 32.969722999999995, 32.97972299999999, 32.98972299999999, 32.99972299999999, 33.00972299999999]

list_b = [35.808097, 35.818096999999995, 35.82809699999999, 35.83809699999999, 35.84809699999999, 35.85809699999999]
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

if you are only considering the lists sizes, i.e., you don't care about the data inside them, you could use the following code:

def compare_and_match(list_a, list_b):
    len_a = len(list_a)
    len_b = len(list_b)

    if len_a > len_b:
        list_a = list_a[:len_b]
    elif len_a < len_b:
        list_b = list_b[:len_a]
    return list_a, list_b


list_a = [32.959723, 32.969722999999995, 32.97972299999999,
          32.98972299999999, 32.99972299999999,
          33.00972299999999, 33.019722999999985, 33.02972299999998]

list_b = [35.808097, 35.818096999999995, 35.82809699999999,
          35.83809699999999, 35.84809699999999, 35.85809699999999]


list_a, list_b = compare_and_match(list_a, list_b)

The expected output would be

>>> list_a
[32.959723,
 32.969722999999995,
 32.97972299999999,
 32.98972299999999,
 32.99972299999999,
 33.00972299999999]

>>> list_b
[35.808097,
 35.818096999999995,
 35.82809699999999,
 35.83809699999999,
 35.84809699999999,
 35.85809699999999]
Daniel Lima
  • 925
  • 1
  • 8
  • 22