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.

user2954367
  • 61
  • 1
  • 7
  • 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. – Hyperboreus Nov 06 '13 at 19:30
  • `duplic_int_lis = int_lis` does not create a duplicate list. – NULL Nov 06 '13 at 19:33
  • @RogerPate .. then how can one go about creating a duplicate list? – user2954367 Nov 06 '13 at 19:41
  • `dupe = list(original)` – NULL Nov 06 '13 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. – Blckknght Nov 06 '13 at 20:05
  • why not just `return int_list[int_list[0]]` – cmd Nov 06 '13 at 21:26

3 Answers3

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
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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

Back2Basics
  • 7,406
  • 2
  • 32
  • 45