Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Adam Ryde
Adam Ryde
5,222 Points

Iterable and values

Hi guys, completed python basics, and i am now starting to work through python collections, i am only on the first part really and i have come across something that is a little confusing.

within the video lesson Kenneth explains in the teachers notes that for .append() a value goes in the parentheses, but on .extend() an iterable goes in the parentheses.

does anyone have a simple way of explaining the difference of a value and an iterable to me in lamens terms please.

i would google this, but i would like to hear it from people on here as i am sure some of you will have thought the same thing at some point also.

many thanks in advance. Adam

3 Answers

Ryan S
Ryan S
27,276 Points

Hi Adam,

A value and an iterable are not necessarily different things. An iterable could be thought of as a type of value. Anytime you create a variable and store something in it, like a number or string or list, that something is a value. So a string is a value that is iterable.

In programming, iteration refers to the process of performing a task or set of tasks over and over. An iterable is something that allows you to loop through its contents and perform those repeated tasks. Examples of an iterable would be a list, dictionary or even a string.

var1 = [1, 2, 3, 4]
var2 = "Treehouse"
var3 = 5

In the above examples, everything to the right of the equal signs is a value, but only the values of var1 and var2 are iterable. You can use a for loop to loop through either the items in the list, or the letters in the string. But you cannot loop through var3 as it is only a single integer.

Hope this clears things up.

Adam Ryde
Adam Ryde
5,222 Points

that does mate, thank you very much i appreciate the visual help aswell, i have to say as i was reading through your explanation and casting my eyes back and to the image of code i was thinking to my self, well if an iterable can be looped through then var1 and var2 are the only ones that could be iterable as 5 is just a single value. hopefully seeing what you have used as an example has hit home and my brain looking at values and iterables this way :)

thanks buddy.

Ryan S
Ryan S
27,276 Points

Hey no problem. Glad it helped. You'll find that eventually all this stuff will come naturally and you won't even need to think about it. You'll automatically know what you can and can't do with certain variable types and won't need to worry so much about the technical definitions.

Happy coding!

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

an iterable is anything that can be iterated or looped through, so a word is iterable, a list, a tuple, a dict etc. a value is a number or letter generally, or really anything, you can append iterables for example to make a list of lists. but generally you will have iterables made up of values, like a word is made up of letters, or a list might be a collection of names, which are in turn made up of letters, etc. you will get an error trying to iterate through an integer as they are not iterable. they must be converted to something that is iterable, like a string.

Adam Ryde
Adam Ryde
5,222 Points

ahhh ok so if i think of values as either letters or numbers (ints) and iterables as strings, lists etc?

would that be a simple way of looking at it at this early stage of my learning?