#๐Ÿ”’ too many items to unpack

75 messages ยท Page 1 of 1 (latest)

hybrid field
#

this is the error

Traceback (most recent call last):
  File "/piston/jobs/84134f09-a047-4c90-818b-68bf6363f496/file0.code", line 11, in <module>
    for j, w in func.items():
AttributeError: 'function' object has no attribute 'items'

this is the code

mylist = ['a','b','c']
def func():
    newlist = []
    otherlist = []
    for i in mylist:
        newlist.append(i)
    for x in range(1, len(newlist)):
        otherlist.append(x)
    return newlist, otherlist

for j, w in func():
    print(j, w)
still grailBOT
#

@hybrid field

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.

hybrid field
#

what can i do for this to return the lists?

prime marsh
#

you are returning lists already?

#

where is the .lists() method call in that snippet?

north shell
#

Why are you calling function without parameters?

hybrid field
hybrid field
little burrow
#

what you want to achieve

hybrid field
#

without raising an error

prime marsh
#

but that code already does it?

little burrow
hybrid field
#

!e
mylist = ['a','b','c']
def func():
newlist = []
otherlist = []
for i in mylist:
newlist.append(i)
for x in range(1, len(newlist)):
otherlist.append(x)
return newlist, otherlist

for j, w in func():
print(j, w)

north shell
still grailBOT
little burrow
#

!e

mylist = ['a','b','c']
def func():
    newlist = []
    otherlist = []
    for i in mylist:
        newlist.append(i)
    for x in range(1, len(newlist)):
        otherlist.append(x)
    return newlist, otherlist
j, w = func()
print(j)
print(w)
prime marsh
#

yeah, that

#

if you just want to get those two lists from func()

little burrow
#

pithink you need unpack it

north shell
little burrow
#

check my above code

hybrid field
#

i want to do it in a for loop

#

like

#

for i x in func():
print(i,x)

little burrow
#

then use zip()

north shell
#

you can also use enumeration

hybrid field
#

recreate it

hybrid field
prime marsh
#

ok, just what do you expect j and w to be on the first iteration of the loop?

#

so we're clear on what you're trying to accomplish here

north shell
#
for count, ele in enumerate(my_list):
    print(count, ele)
hybrid field
#

im trying to recreate enumerate myself

north shell
#
my_list = ['a','b','c']
cnt = 0

def func(some_list):
    for i in some_list:
        yield i

for i in func(my_list):
    print(i, cnt+1)
    cnt=cnt+1

#

?

#

you can add counter to the function and just add +1

hybrid field
#

whats yield?

torpid bear
#

yield is return but with the added option to resume execution

north shell
hybrid field
#

hm

#

im lost

#

wait

north shell
#
def func(some_list):
    cnt = 0
    for i in some_list:
        cnt = cnt+1
        yield i, cnt

for i, j in func(my_list):
    print(i, j)



#

I suppose this is what you want to achieve, right?

hybrid field
#

yea

north shell
#

so you should use yield

hybrid field
#

so if i do return it raises an error?

north shell
#

you do not return here, you yield

hybrid field
#

but with yield it doesnt?

north shell
#

there isn't really any possibility to achieve an error

hybrid field
#

!e

def func(some_list):
    cnt = 0
    for i in some_list:
        cnt = cnt+1
        yield i, cnt
my_list = ['a','b','c']
for i, j in func(my_list):
    print(i, j)



still grailBOT
hybrid field
#

okay

north shell
#

you kind of "return" (yield) two values and you can for loop using in func(my_list)

hybrid field
#

so how do i know when to use return and when to use yield?

torpid bear
#

you probably want to ignore yield for now

#

if you're creating pairs of n, x, where x is a value and n is an index

#

then what you have is *many* *pairs*

#

whenever you have the concept of *many* - the default structure for that is *list*

hybrid field
#

alright

#

there really is much to learn

torpid bear
#

[(0, a), (1, b), (2, c), ...]

hybrid field
#

thanks for the help!

torpid bear
#

you had this:

[0, 1, 2, 3]
[a, b, c, d]
#

which is slightly off, since what you have is pairs, not two separate "many's"

still grailBOT
#
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.