Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:02:25+00:00 2026-06-15T06:02:25+00:00

I have a condition that I’m checking against each element in a list and

  • 0

I have a condition that I’m checking against each element in a list and if the condition becomes true than I want to increment the value of a counter in one of several variables. Here’s my example:

for el in elements:
    if sum(x, el) < 5:
        .........DO SOMETHING: Increment variable age

Now if I want to increment the variable that relates to that el in elements how would I do it. So for example if the el was age and then I want to increment an age variable how would this be done?

I could do this by writing lots of switch statements, but what would be the most pythonic way to do it.

Thanks

EDIT

What I’m looking to do is that each element is a set of coordinates of a city(Long, Lat) and for each one I’m calculating the distance from an event. I’ll be using geopy.

Now as each element is coordinates not a city name. I want to say if the event is less than 5km from the coordinates of New York to increment the New York variable or if it’s 5km from London to increment the London variable.

Hope this makes it more clearer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:02:27+00:00Added an answer on June 15, 2026 at 6:02 am

    This is a case for a list comprehension:

    [el if sum(x, el) < 5 else el + 1 for el in elements]
    

    Edit: From your edit, I’m not really sure what you are describing matches what you said originally, what your edit says matches something like this more closely:

    cities = {"London": (10, 15), "New York": (20, 50)}
    def find_closest_city(location):
        ...
    
    city_count = collections.Counter([find_closest_city(el) for el in elements])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add an element to a BinarySearchTree. I have a condition that
I have the below coding, i am make sure that if condition is TRUE,
I have a script that I want to exit early under some condition: if
I have a script that I want only to load if a certain condition
I have a problem with an if condition that I want to use within
I have a multiple selection wxListBox and I want to keep the condition that
I want to have a condition that will perform some action when the row
Sometimes I have to check for some condition that doesn't change inside a loop,
Possible Duplicate: php false place in condition I have noticed that a lot of
I have an issue that looks like a race condition with a webview callback

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.