1

Hello my fellow programmers.

I am a fairly new programmer, and now I am facing a great predicament. I am getting the error:

can only assign an iterable

Firstly I don't know what that means.

Secondly I will leave my code for you professionals to critique it:

def num_top(int_lis):
    duplic_int_lis = int_lis
    int_firs= duplic_int_lis [0]
    int_lis[:] = duplic_int_lis [int_firs]

Basically I am trying to find the [0] element in the list and then using that int as an index position to find the integer at that index position.

6
  • You are trying to assign an integer (I guess from the name) to a list. As the error says: You can only assign to lists (or slices thereof) an iterable and an integer is not iterable. Commented Nov 6, 2013 at 19:30
  • duplic_int_lis = int_lis does not create a duplicate list. Commented Nov 6, 2013 at 19:33
  • @RogerPate .. then how can one go about creating a duplicate list? Commented Nov 6, 2013 at 19:41
  • dupe = list(original) Commented Nov 6, 2013 at 19:42
  • 1
    Can you clarify what return value you expect from your function (it currently returns None) and what mutation you expect to happen to the input list? Your example output could be either of those, but we can't tell which. Commented Nov 6, 2013 at 20:05

3 Answers 3

4

int_lis[:] = duplic_int_lis [int_firs] means assign all the items of duplic_int_lis [int_firs] to int_lis, so it expects you to pass an iterable/iterator on the RHS.

But in your case you're passing it an non-iterable, which is incorrect:

>>> lis = range(10)
>>> lis[:] = range(5) 
>>> lis               #all items of `lis` replaced with range(5)
[0, 1, 2, 3, 4]

>>> lis[:] = 5        #Non-iterable will raise an error.
Traceback (most recent call last):
  File "<ipython-input-77-0704f8a4410d>", line 1, in <module>
    lis[:] = 5
TypeError: can only assign an iterable

>>> lis[:] = 'foobar' #works for any iterable/iterator
>>> lis
['f', 'o', 'o', 'b', 'a', 'r']

As you cannot iterate over an integer, hence the error.

>>> for x in 1: pass
Traceback (most recent call last):
  File "<ipython-input-84-416802313c58>", line 1, in <module>
    for x in 1:pass
TypeError: 'int' object is not iterable
Sign up to request clarification or add additional context in comments.

3 Comments

okay I got the code working... but now the problem is... nothing mutates... it still returns the original list.
@user2954367 Your question isn't clear actually, please provide some examples in the question body.
@user2954367 Try int_lis[int_lis[0]].
1

The RHS of a slice-assignment must be an iterable, not a scalar. Consider slice-deleting and then appending instead.

6 Comments

while technically impeccable i'm afraid i can't upvote an answer that is 98% likely to go over the questioner's head...
no that actually makes sense, only thing is... could you explain slice deleting?
@user2954367: del int_lis[:] This deletes the contents of the list, but leaves the list itself alone. But reading further, it seems that this isn't what you want to do at all.
well that is sort of what I want.. But I want the integer without the [].. for example now when I run the code the result I get is [3].
Then you should return the value instead of mutating the list.
|
0

An iterable is a thing with multiple items that you can iterate through (for example: take the 1st value do something, then the 2nd do something, etc...) Lists, dictionaries, tuples, strings have several items in them and can be used as iterables. As a counterexample: number types don't qualify as iterable.

Remember that computers count from #0 so: if you want the first value of a list you can use

my_list[0]

before you go further I would suggest watching this video about looping. https://www.youtube.com/watch?v=EnSu9hHGq5o

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.