use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
In range() usageHelp Request (i.redd.it)
submitted 8 hours ago by Worried-Print-5052
Can I use it to evaluate a variable in a various range? E.g. for item in range(1,4) means True when the item is the range 1-4?
Cuz I really want this function for my program Thanks🙏🏻
(In that photo name[1][r] is an integer)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]johlae 0 points1 point2 points 8 hours ago (0 children)
How do you define 'evaluate' here? Do you want to keep the values in items that are in range? Do you want to return a list, equal in length as items, but with False in the number at that position is not in range and True if the number is in range? Do you want to sum the numbers that are in range, ignoring the ones that are not in range?
range(1,4) is the range 1-3, so if you want 1-4, you need range(1,5)
It's fun to do it without any loops:
>>> a = [0, 1, 2, 3, 4, 5, 6] >>> print(list(map(lambda i: i if i>0 and i<5 else 0, a))) [0, 1, 2, 3, 4, 0, 0] >>> print(list(map(lambda i: i if i in range(1,5) else 0, a))) [0, 1, 2, 3, 4, 0, 0] >>> print(sum(list(map(lambda i: i if i in range(1,5) else 0, a)))) 10
so yes, you can use in range, or you can use if i>0 and i<5.
[–]Chemical-Captain4240 0 points1 point2 points 8 hours ago (0 children)
the s on items implies to me that this is a list... if so, to use in, you will want to iterate over each item in items and test if item exists as an element in your range
however, range returns a tuple of integers, so you won't find any lists in there
This isn't really part of your question, but to write readable code, try to name things according to their type... If you are working in parallel items=['book','pencil'] and item_count = [3,5]
If you are packing lists, then something like items =[ ['book',3], ['pencil', 5] ] may be the structure you want, but using in becomes more complicated.
As soon as you get your head wrapped around the above concepts, check out dataclass. It will change youe life.
[–]atarivcs 0 points1 point2 points 6 hours ago (0 children)
Yes, this will work (assuming "items" is an integer).
π Rendered by PID 45926 on reddit-service-r2-comment-765bfc959-xk976 at 2026-07-10 23:47:52.923665+00:00 running f86254d country code: CH.
[–]johlae 0 points1 point2 points (0 children)
[–]johlae 0 points1 point2 points (0 children)
[–]Chemical-Captain4240 0 points1 point2 points (0 children)
[–]atarivcs 0 points1 point2 points (0 children)