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 8533375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:57:04+00:00 2026-06-11T09:57:04+00:00

Just wrote some nasty code that iterates over a dict or a list in

  • 0

Just wrote some nasty code that iterates over a dict or a list in Python. I have a feeling this was not the best way to go about it.

The problem is that in order to iterate over a dict, this is the convention:

for key in dict_object:
    dict_object[key] = 1

But modifying the object properties by key does not work if the same thing is done on a list:

# Throws an error because the value of key is the property value, not 
#     the list index:

for key in list_object:
    list_object[key] = 1 

The way I solved this problem was to write this nasty code:

if isinstance(obj, dict):
    for key in obj:
        do_loop_contents(obj, key)
elif isinstance(obj, list):
    for i in xrange(0, len(obj)):
        do_loop_contents(obj, i)

def do_loop_contents(obj, key):
    obj[key] = 1

Is there a better way to do this?

Thanks!

  • 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-11T09:57:05+00:00Added an answer on June 11, 2026 at 9:57 am

    I’ve never needed to do this, ever. But if I did, I’d probably do something like this:

    seq_iter = x if isinstance(x, dict) else xrange(len(x))
    

    For example, in function form:

    >>> def seq_iter(obj):
    ...     return obj if isinstance(obj, dict) else xrange(len(obj))
    ... 
    >>> x = [1,2,3]
    >>> for i in seq_iter(x):
    ...     x[i] = 99
    ... 
    >>> x
    [99, 99, 99]
    >>> 
    >>> x = {1: 2, 2:3, 3:4}
    >>> for i in seq_iter(x):
    ...     x[i] = 99
    ... 
    >>> x
    {1: 99, 2: 99, 3: 99}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble with some code I just wrote. Basically, this code is inside
I just wrote some code, looked it over, and wanted to cry. Lines that
I just wrote this piece of code but I'm not quite happy about it.
I just wrote some code. It has my own custom made class. In that
I'm running some test to prove a concept and i just wrote this code
I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y))
I just wrote some code that I don't quite comprehend how it works. My
I just wrote some code that works very well. But I feel that it
I just wrote some JavaScript code that follows along with what I believe to
We have a certain database that has some employee information, and I just wrote

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.