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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:32:52+00:00 2026-06-07T05:32:52+00:00

So my main goal is simple, I want multiple values to be returned when

  • 0

So my main goal is simple, I want multiple values to be returned when using a single key. However I’m getting errors and confusing behavior. I am new to Python, so I fully expect there to be a simple reason for this issue.

I have a list of objects, list which only contains the index of the object. i.e.

1
2
3
4 
etc.. 

and a file containing the groups that each of the objects belong to, listed in the same order. The file is a single value for n lines (n being the length of the list of objects as well.) i.e. the file looks like this:

2
5
2
4
etc..

meaning the first object belongs in group 2, the second object in group 5, the third in group 2 and fourth in group 4. This file will change depending on my input files. I have attempted the two following suggestions (that I could find).

EDIT: my end goal: to have a dictionary with group numbers as the keys and the objects in the groups as the values.

I looked to this StackOverflow question first for help since it is so similar, and ended up with this code:

def createdDict(list, file): 

    f = open(file, 'r')
    d={}
    i=0 

    for line in f:
        groupIndex = int(line)
        if groupIndex in d:
            d[groupIndex].append(list[i])
        else:
            d[groupIndex] = list[i]
        i +=1
    print d
    f.close()

And this error:

AttributeError: 'Element' object has no attribute 'append'

d[groupIndex] is just a dictionary and its key and groupIndex should also just be an integer.. not an object from a class I created earlier in the script. (Why is this error showing up?)

I then revised my code after coming upon this other question to be like the following, since I thought this was an alternative way to accomplish my task. My code then looked like this:

def createdDict(list, file): 

    f = open(file, 'r')
    d={}
    i=0 

    for line in f:
        groupIndex = int(line)
        if groupIndex in d:
            d.setdefault('groupIndex', []).append(list[i])
        else:
            d[groupIndex] = list[i]
        i +=1
    print d
    f.close()

This code snippet doesn’t end in an error or what I want, but rather (what I believe) are the last objects in the groups… so print d gives me the key and the last object placed in the group (instead of the desired: ALL of the objects in that group) and then terminal randomly spits out groupIndex followed by all of the objects in list.

My question: what exactly am I missing here? People upvoted the answers to the questions I linked, so they are most likely correct and I am likely implementing them incorrectly. I don’t need a correction to both procedures, but the most efficient answer to my problem of getting multiple values attached to one key. What would be the most pythonic way to accomplish this task?

EDIT 2: if this helps at all, here is the class that the first method is referencing the error too. I have no idea how it defined any part of this code as a part of this class. I haven’t really developed it yet, but I’m all for an answer, so if this helps in locating the error:

class Element(object):
    def __init__(self, globalIndex):
        self.globalIndex = globalIndex
    def GetGlobalIndex (self):
        return self.globalIndex

globalIndex is a separate index of objects (Elements). with my current problem, I am taking a list of these Elements (this is the list mentioned earlier) and grouping them into smaller groups based upon my file (also mentioned earlier). Why I thought it shouldn’t matter, the list is essentially a counting up of integers… How would it mess with my code?

  • 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-07T05:32:53+00:00Added an answer on June 7, 2026 at 5:32 am

    The base of your problem is in this line:

    d[groupIndex] = list[i]
    

    In other words, when a key is not in the dictionary, you add a single value (Element object) under that key. The next time you see that key, you try to append to that single value. But you can’t append to single values. You need a container type, such as a list, to append. Python doesn’t magically turn your Element object into a list!

    The solution is simple. If you want your dictionary’s values to be lists, then do that. When you add the first item, append a one-element list:

    d[groupIndex] = [list[i]]
    

    Alternatively, you can take a one-item slice of the original list. This will be a list already.

    d[groupIndex] = list[i:i+1]
    

    Now the dictionary’s values are always lists, and you can append the second and subsequent values to them without error.

    As ecatmur points out, you can further simplify this (eliminating the if statement) using d.setdefault(groupIndex, []).append(list[i]). If the key doesn’t exist, then the value is taken to be an empty list, and you simply always append the new item. You could also use collections.defaultdict(list).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question that is confusing me. Goal : I want to
I've recently started my new private project. The main goal which I want to
I have a relatively simple goal: I want to create a Cocoa application which
I'm mainly interested in the simple goal of getting a C program to I
Is there a Maven phase or goal to simply execute the main method of
The main goal of my issue is to put specific colors on Donut Change.
My main goal is to change the volume on one sound card based on
I have an application thats main goal is to play a specific video file.
How do i do that? Actually my main goal is to get which checkbox
For homework, I need to build a small java application.The main goal is to

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.