+ 2

Hello everyone, here is my question: if i want to count how many numbers are, before and after a single item in a list

E.g items = [1, 4, 5, 7, 3, 6, 9, 0, 10] So here, i want to count numbers before 3 in the items and after 3. How can I do it in python? Pls help me out.

9th Sep 2022, 10:44 AM
© Mustapha Khalid Yakubu 🇳🇬
© Mustapha Khalid Yakubu 🇳🇬 - avatar
13 Answers
+ 11
9th Sep 2022, 11:15 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 9
To do this, you should find the index of the desired number (for example, 3). Then, break the array into two subarrays around that index and get the length of both of them. This is best done by indexing using the : operator, as seen in my code below. https://code.sololearn.com/cCbt5XBhn4q0/?ref=app
9th Sep 2022, 11:00 AM
Daniel C
Daniel C - avatar
+ 6
here is a version that uses python *more_itertools* module, that has some great iteration tools. in this case i used *split_at()* from more_itertools import split_at items = [1, 4, 5, 7, 3, 6, 9, 0, 10] inp = int(input()) print([len(i) for i in list(split_at(items, lambda n: n == inp))]) result: [4, 4] the interesting thing is: if the input list contains more then 1 of the separator, it also works great: items = [1, 4, 5, 7, 3, 6, 9, 3, 0, 10] # 3 occurs 2 times in the list result: [4, 2, 2]
9th Sep 2022, 5:46 PM
Lothar
Lothar - avatar
+ 6
Expanding on Lothar's solution, and making more general purpose: https://code.sololearn.com/cOH3HAQmaqfG/?ref=app
10th Sep 2022, 10:25 AM
Steve
Steve - avatar
+ 5
lst=[1, 4, 5, 7, 3, 6, 9, 0, 10] strlst= str(lst).replace(", 3,","],[") eval(f"print([len(i) for i in [{strlst}]])")
9th Sep 2022, 12:02 PM
Oma Falk
Oma Falk - avatar
+ 5
Lothar 😎😎😎 itertools is so under-utilized. more_itertools, even more under-utilized. Learn Python this way. It is good to implement your own methods when learning, but please also learn the tons of useful modules Python have. They will spare you a lot of pain as your code becomes more complex.
11th Sep 2022, 3:02 AM
Bob_Li
Bob_Li - avatar
+ 3
Chidiebere Ogbuagu The basics are easy to learn. About a month at most. But learning is an ongoing process. Coding languages are continually being updated, features added and deprecated. So one tries to keep up to date, and try to learn and sometimes to unlearn things.
11th Sep 2022, 3:15 AM
Bob_Li
Bob_Li - avatar
+ 2
10th Sep 2022, 10:05 AM
Emmanuel Odukoya
Emmanuel Odukoya - avatar
+ 1
Thanks Rik Wittkopp, that's what I needed
9th Sep 2022, 3:49 PM
© Mustapha Khalid Yakubu 🇳🇬
© Mustapha Khalid Yakubu 🇳🇬 - avatar
+ 1
Chidiebere Ogbuagu I have learned 3 programming languages(bash, python, JavaScript) within 20 months. My goal is not to be a developer but to be able to write, and understand codes/scripts in different programming languages. There is no actual length of learning, it depends on hours you dedicate to learn and practice.
10th Sep 2022, 2:27 PM
bakeery
+ 1
👍🏼👍🏼
11th Sep 2022, 8:33 AM
ANISIOFOR IFUNANYA PRINCE 🇳🇬
ANISIOFOR IFUNANYA PRINCE 🇳🇬 - avatar
0
How long did all of u learn to code
10th Sep 2022, 7:04 AM
Chidiebere Ogbuagu
Chidiebere Ogbuagu - avatar
0
Use the index function of Python
11th Sep 2022, 8:08 AM
Henry Pebbles Tutorials
Henry Pebbles Tutorials - avatar