Python - In and Not In List Syntax Error


I'm trying to construct a new list of floats from another existing list of floats. The expected contents of that first list are easier to identify by example:

price_list = [39.99, 74.99, 24.99, 49.99]

Expected, post function:

print new_price_list >>[29.99, 34.99, 44.99, 54.99, 59.99, 64.99, 69.99]

The new list is derived by looking at the range of the existing list and, beginning with the minimum value in the existing list, appending the floats += 5.00 that aren't in the existing list. My initial attempt at a solution was:

price_list = [39.99, 74.99, 24.99, 49.99] min_price = min(price_list) new_price_list = [] while min_price < max(price_list): if min_price not in price_list: new_price_list.append(min_price) min_price += 5.00 for price in new_price_list: print price >>29.99 >>34.99 >>39.99 >>44.99 >>49.99 >>54.99 >>59.99 >>64.99 >>69.99

And just for reference:

print new_price_list >>[29.99, 34.989999999999995, 39.989999999999995, 44.989999999999995, 49.989999999999995, 54.989999999999995, 59.989999999999995, 64.99, 69.99]

In the meantime, I've identified what I think is an issue with how the min_price is being compared to the items in price_list. My awkward workaround solution is as follows. However, I am still curious if there is anyway to more efficiently accomplish this task, as I was seeking to do in my original guess at a solution, or perhaps even more using a list comprehension even with the min_price += 5.00?

price_list = [39.99, 74.99, 24.99, 49.99] min_price = min(price_list) new_price_list = [] while min_price < max(price_list): if int(min_price) not in [int(price) for price in price_list]: new_price_list.append(int(min_price)) min_price += 5.00 better_price_list = [price + 0.99 for price in new_price_list] print better_price_list [29.99, 34.99, 44.99, 54.99, 59.99, 64.99, 69.99]

Thanks very much for your help! Looking forward to getting to know this community better.


To generate the values, the min and max and create a custom range generator:

mn = min(price_list) mx = max(price_list) def flt_rnge(start, stop, step): start += step while start < stop: yield start start += step print(list(flt_rnge(mn, mx, 5)))

Which outputs the following which is not a <em>syntax error</em>, it is the repr output:

[29.99, 34.989999999999995, 39.989999999999995, 44.989999999999995, 49.989999999999995, 54.989999999999995, 59.989999999999995, 64.99, 69.99]

If you wanted value not already in your list you could use a set to store the prices already in your list but you are going to run into <a href="https://docs.python.org/2/tutorial/floatingpoint.html#floating-point-arithmetic-issues-and-limitations" rel="nofollow">floating-point-arithmetic-issues</a> when comparing the floats, in that case and always when dealing with money you should use the <a href="https://docs.python.org/2/library/decimal.html#module-decimal" rel="nofollow">decimal</a> module:

price_list = [39.99, 74.99, 24.99, 49.99] mn = min(price_list) mx = max(price_list) from decimal import Decimal def flt_rnge(start, stop, step, l): st = map(str, l) start,stop,step = Decimal(str(start)),Decimal(str(stop)),Decimal(str(step)) start += step while start < stop: if start not in st: yield start start += step print(list(flt_rnge(mn, mx, 5, price_list))) [Decimal('29.99'), Decimal('34.99'), Decimal('44.99'), Decimal('54.99'), Decimal('59.99'), Decimal('64.99'), Decimal('69.99')]

You are printing the output in your first part of the question so you see the nicely formatted output, when you print the list after you are seeing the repr which shows the float value stored is not actually equal to xx.99 so all your if x not in list.

When dealing with money, you should use the decimal module from the start.


Something like this is probably what you want..

upper_bound = max(int(p) for p in price_list) lower_bound = min(int(p) for p in price_list) new_price_list = [(p+0.99) for p in range(lower_bound, upper_bound, 5) if (p+0.99) not in price_list]


This is an other way to achieve what you need:

price_list = [39.99, 74.99, 24.99, 49.99] min_price = min(price_list) max_price = max(price_list) new_price_list = [min_price + 5.00] for i in new_price_list: if i < max_price - 5.00: num = i+5.00 num = round(num,2) new_price_list.append(num) print new_price_list


[29.99, 34.99, 39.99, 44.99, 49.99, 54.99, 59.99, 64.99, 69.99]


Are you really guaranteed that the prices will all end in .99 ? If not, then your solution and the new proposal will both fail on actual data.

I suggest that you first convert the prices to cents:

pennies_list = [round(100*p) for p in price_list]

<em>Now</em> you can go about your usual business of finding the missing prices. Find the min_price and max_price values of pennies list, as polpak showed.

full_list = [p for p in range(min_price, max_price, 500) ] original_set = set(pennies_list) full_set = set(full_list) new_set = full_set - original_set new_price_list = list(new_set)


  • Flask application fails after updated Azure service plan
  • SharpSVN error in VS2010
  • setvbuf() - size parameter when buf is NULL
  • Android studio 3.1 rebuild project each time to see the changes
  • Socket.io on socket disconnect
  • Hakyll says “Dependency cycle detected: …”
  • Scrolling News Ticker Jquery - Issues
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Catching exception and throwing the same?
  • Android getAssets in Common class gives “The method getAsssets() is undefined for the type Context”
  • JsonConverter: Get parent object in ReadJson, without $refs
  • Negated scanset in fscanf and EOF
  • How to make HttpWebRequest async
  • How to protect an asp:textbox from user input?
  • possible limitation of implode function in PHP
  • CKeditor stripping font tags instead of converting to span
  • Cursor in wrong place in contenteditable
  • Adding independent aspx/asmx pages into DotNetNuke
  • findObjectsInBackgroundWithBlock block signature not correct
  • What is the best way to debug Bootstrap.groovy?
  • Mocha throws unexpected token error for ES6 object spread operator
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • Deploying a CodeRush plugin from the Community Site
  • Local Development, Apache vs Developer - file permissions
  • ckeditor and jquery UI dialog not working
  • Less Conflicting Session Manager for Zope 2
  • mave 3.2 not able to access local nexus instance return 502 code
  • Can I use AllJoyn Framework for Wifi Direct in iOS?
  • redirect_to root_url and return unless @user.activated
  • Cloud Code function running twice
  • Can't remove headers after they are sent
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • How does document.ready work with angular element directives?
  • Webgrid not refreshing after delete MVC
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • $wpdb not working in file of WordPress plugin
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Data Validation Drop Down Box Arrow Disappearing