-1

I have a list that's is enumerated and I wish to use modular arithmetic on each number, but not the enumerated numbers. How do I do this? This question regards only list modular arithmetic not a simple operation.

How do I mod 121 each of these numbers in the list (236, 242, 248) all at once?

    lst1 = [22, 236, 23, 242, 24, 248]
    print(mod 121 lst1)
Tom E. O'Neil
  • 527
  • 4
  • 10

2 Answers2

1

List comprehensions are the typical way to perform an operation on each element in an input iterable, returning a new list:

print([x % 121 for x in lst1])

That's roughly equivalent to:

newlst = []
for x in lst1:
    newlst.append(x % 121)
print(newlst)

but should run significantly faster, and (for simple cases like this) is easier to read.

Update: It seems like you only want to apply this to every other element in the list, so just slice it down before performing the comprehension:

print([x % 121 for x in lst1[1::2]])

lst1[1::2] is a slice that makes a new list with only the odd-index elements (1, 3, 5, ...), so you only operate on [236, 242, 248].

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Yes but I don't want to mod the enumerated numbers thanks – Tom E. O'Neil Nov 14 '19 at 21:22
  • @TomE.O'Neil: Huh? You need to describe your problem better then, including desired output, because even rereading your question, I can't figure out what else you might want. – ShadowRanger Nov 14 '19 at 21:25
  • @TomE.O'Neil: Made a guess at what you meant (the even indices are the "enumerated" numbers, the odds are the "values"). That handle it? – ShadowRanger Nov 14 '19 at 21:30
  • Hi shadowRanger, you have mod 121 in the list yet I need to maintain and still see the enumerated numbers. I'm working on number theory at the moment for prime numbers and I have found a formula which finds factors so any help with maintaining the enumerated numbers and only mod 121 the numbers after every enumerated number is helpfule. thanks – Tom E. O'Neil Nov 14 '19 at 21:56
  • @TomE.O'Neil: I can't make sense of your requirements at all. Please edit in a few example inputs and desired outputs into your question. – ShadowRanger Nov 14 '19 at 22:52
  • ShadowRanger you have solved it and I gave you a point thanks can you please have a stab at this question? https://stackoverflow.com/questions/58868162/how-do-i-subtract-11-from-a-list-and-mod-121-the-same-list-all-at-once?noredirect=1#comment104005575_58868162 – Tom E. O'Neil Nov 14 '19 at 23:39
0

This can be accomplished with a lambda and map method:

mod_lamda = lambda x: x % 121
lst1 = list(map(mod_lambda, lst1))

Or this could be accomplished iteratively

for i in range(len(lst1)):
  lst1[i] = lst1[i] % 121
  • 1
    Any use of `list(map(...))` that relies on a `lambda` as the mapping function will lose to an equivalent list comprehension that can inline the operation from the `lambda` (avoiding the function call overhead entirely). `map` only pulls off meaningful wins when it can use an existing function implemented in C (and thereby avoid bytecode execution entirely). – ShadowRanger Nov 14 '19 at 21:24
  • Good to know. Thanks @ShadowRanger! – darrienkennedy Nov 14 '19 at 21:27