#๐Ÿ”’ looping through nested dict in a list

74 messages ยท Page 1 of 1 (latest)

late perch
#

Here i am trying to have an input of a course name, and whoever has that course in their info, pop up in the format person - identify, but it says that a 'set' object is not subscriptable, what does that mean?

people = []
info = input().split(':')

while len(info) > 1:
    for i in range(0, len(info), 3):
        name = info[i]
        identify = int(info[i+1])
        course = info[i+2]
        new_person = name, identify, course
        people.append({new_person})
    info = input().split(':')
else:
    for person in [person for person in people if info[0] in {person['course']}]:
        print(f'{person["name"]} - {person["identify"]}')

idle ironBOT
#

@late perch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

late perch
#

the code gets info in the format of
John:5622:fundamentals (example)
and splits it into name, id and course, then put on a list

#

and if we get a text that isnt split, then that's the course name we're supposed to find which students have and it should begin searching for any person with that course

#

example output

#

the error is in people if info[0] in {person['course']}]:

still knoll
#

the error is in people.append({new_person}), effectively, that is a set with 1 element

#

what did you mean by it?

late perch
still knoll
#

if you want to make a dictionary you're going to need to well, construct it

still knoll
#

like {"name": name} whatever

still knoll
late perch
#

oh..

#

i have the key:value, 2ndvalue

#

i just havent formatted it correctly ig

still knoll
#
        new_person = name, identify, course
        people.append({new_person})

new_person is a tuple of 3 values, {new_person} is a set with that tuple, so yeah, you'll want to change how you define new_person, and remove the {} from the thing you append, as its going to already be a dict

rough totem
#

There are more is more than one problem tho

still knoll
#

dict(new_person) will error

late perch
#

soo make new_person = {}

#

and then add to it

#

then finally add to list

#

?

still knoll
late perch
#

how?

still knoll
#

{"name": name, "age": age, ...}
im not sure why do you want to use it though here, as you are going to like, use it right away, and you're creating the data yourself, so you're just adding a level of indirection/complexity by doing that instead of a tuple/whatever

late perch
still knoll
#

thats fine then

late perch
#

but everything has to be inside of the dict stuff :D

still knoll
# still knoll thats fine then

if you want to define a structure that like just stores a bunch of specific data - you can use classes (dataclasses) for this
but learning dicts is still good, they're really useful, maybe not in this case but overall

late perch
#

ah i have an idea

still knoll
late perch
#

give me a second

rough totem
#

Yo my bad, there is two ways of doing it actually


people.append(dict(zip(['name', 'identify', 'course'], new_person)))

# This will create a dictionary with the keys name, identify, and course, and the corresponding values from the new_person tuple.

# Alternatively, you can use a dictionary literal to create the dictionary, like this:

people.append({'name': name, 'identify': identify, 'course': course})

#Either of these approaches should fix the error and allow you to access the elements of the dictionary using the square bracket notation.
late perch
#
        name = info[i]
        identify = int(info[i+1])
        course = info[i+2]
        new_person = {'name': name, 'identify': identify, 'course': course}
        people.append(new_person)```
#

oh just like you made it now

#

perfect

rough totem
#

Nice

#

๐Ÿ‘

late perch
#

could save a line on new_person also

#

thanks a lot, both of you!

#

the info with split : is just the exercise

#

thats how they input it

#

Peter:123:fundamentals

#

cheers! works perfectly

rough totem
#

Np, any question just dm or ask here ๐Ÿ™‚

late perch
#

but the reader seems to dislike something

#

gives me an error that pycharm wouldnt give

rough totem
#

Send it pls

late perch
#

nevermind

#

i posted the solution to a wrong exercise

#

been programming like 8 hours today i am halfway braindead

rough totem
#

Xd

#

What course you doing?

late perch
#

hmm..

#

this just seems wrong on their side

late perch
#

Softuni

#

but its the best one here

rough totem
#

Can you pass me the full problem?

late perch
#

oh i ignored the snake case part

#

right..

rough totem
#

xD

#

Fixed?

late perch
#

yup! easy solution

#

just replace '_' with ' '

rough totem
#

Noice

late perch
#

thanks alot, once again! :D

#

!close

idle ironBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.