-2
# Creating List of integers
List = [112, 966, 483, 807, 112, 693, 507, 712, 738, 605, 923, 112, 966, 679, 992, 29, 782, 780, 353, 392]

print("\nList of numbers: ")

print(List)

# Create Divide variable
myInt = 13

#List values divided by 13
newList = [x / myInt for x in List]

print("\nList Divided by 13")

print (newList)

I'm trying to make a new list now that is only of the sum of the division from my listed newList but i'm want only to keep the integers, How would I filter it out?

Or I guess what might be easier, if just listing how many numbers total can be divided by myInt and then output it as an integer?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dave
  • 1
  • 2
  • Check out this [post](https://stackoverflow.com/questions/52094533/filter-integers-in-numpy-float-array). It explains how to get only the integer values of an array (after your division) and then to get your result, you simply find the length of that filtered array. – Jacob K Sep 23 '20 at 14:42
  • Please work through a short tutorial on the modulus operator, `%` – Prune Sep 23 '20 at 14:43

2 Answers2

1

You can use the modulo operator. someNum % yourInt will not give a remainder if it is divisible, therefore it will return 0. you can use that to filter.

if a % myInt == 0:
    # add to list 

you can find more info here: https://www.geeksforgeeks.org/what-is-a-modulo-operator-in-python/ ( or just searching the modulo operator )

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You'll need to check your newList value by using if in list

newList = [int(x / myInt) for x in List if x/myInt % 1 == 0 ]

The result will be [39, 71, 60]

Or you want each numbers integer part you can use

newList = [x // myInt for x in List ]

Take a look at This post.

Darkborderman
  • 129
  • 3
  • 7
  • Thanks, but trying to understand the last part, the if statement you're saying to put in, is the %1 ==0 making so anything that doesn't decimals is shown? – Dave Sep 23 '20 at 14:56
  • Decimal is not shown because `int(x / myInt)` remove the decimal part. This line meaning is like ` get each item in sequence in myList as x, and if x mod myInt equal to 0, convert it to int then put it in response(newList)` If a values has decimal part it will be filtered out since %1 is not 0` – Darkborderman Sep 23 '20 at 14:59
  • Awesome thanks for explaining! Was wondering on why that was and I haven't touched on modulus operations yet so I wasn't understanding some other post/ answers – Dave Sep 23 '20 at 15:07