5 Replies - 506 Views - Last Post: 23 February 2016 - 04:23 PM Rate Topic: -----

#1 maglev   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 26-September 15

calling list indices within a for loop?

Posted 22 February 2016 - 11:50 AM

lst = [1,3,5,6,7,8]

I was working on this on my free time-
for item in range( len(lst) - 1 ):
    #how do they call list indices
    if lst[item+1] - lst[item] == 1:






ultimately this was not my implementation but something i saw in a video lecture-
in order to resolve a new list or update the current list with a incrementing sequence i.e [1,2,3,4,5...]
but it made me want to ask a couple of things, i know a for loop needs to iterate through 'something'
whether its a list or some other sequence but-
1. how is it possible to use 'item' in this case as a reference for a list index-
2. what other things can 'item' have in it- can be called for or from??
3. can item have its own list indexes and can those be called??? is this an inception like question??

These last two questions probably sounds nuts in hindsight-
but there has to be some other things going on under the hood that I am totally unable to understand completely.

Lastly, as always- thanks for the read! hope you are enjoying your pythoning as much as I am.

-M

This post has been edited by maglev: 22 February 2016 - 11:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: calling list indices within a for loop?

#2 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6560
  • View blog
  • Posts: 26,597
  • Joined: 12-December 12

Re: calling list indices within a for loop?

Posted 22 February 2016 - 01:16 PM

I am not certain what you are asking.

1. how is it possible to use 'item' in this case as a reference for a list index

This is what your code is already doing(?).

Rather than using range() you can use enumerate() if you want to iterate both the items and their index:
for index, item in enumerate(sequence):

see here

Here's a fuller tutorial, For Loops.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7197
  • View blog
  • Posts: 15,003
  • Joined: 16-October 07

Re: calling list indices within a for loop?

Posted 22 February 2016 - 01:16 PM

I'm not really following. Perhaps you're looking for:
>>> for i,x in enumerate([1,3,5,6,7,8]): print(i,x)
...
0 1
1 3
2 5
3 6
4 7
5 8
>>>



Or, maybe:
>>> xs = [1,3,5,6,7,8]
>>> [ x for x in zip(xs, xs[1:]) ]
[(1, 3), (3, 5), (5, 6), (6, 7), (7, 8)]
>>>



Which could then be followed with:
>>> [(x,y) for (x,y) in zip(xs, xs[1:]) if y - x == 1 ]
[(5, 6), (6, 7), (7, 8)]
>>>


Was This Post Helpful? 0
  • +
  • -

#4 maglev   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 26-September 15

Re: calling list indices within a for loop?

Posted 22 February 2016 - 01:29 PM

Hey andrewsw -

probably not enough sleep is the problem-

having understood that item in this case -is just a source for a sequence of numbers 0 1 2 etc-
so question #1 was really just me not understanding what the code was doing initially.

had never saw someone using a for loop to call list indexes in that way-

i've used enumerate in a for loop to get a 'listy' order of things-
but never for iterating both things at once- say like in a dictionary with -
for k,v in my_dict.items():
...


was only instance of exposure where two things are iterated simultaneously-

question #2 was from not realizing where the data is being drawn from-
its being drawn from lst- not from the ending of the declaration of the for loop
in the previous code- it was iterating through a range of numbers and not the list itself-

still think that question 3 is a little nutty but i guess in practice it would look like this-
albeit probably confusing after a while

for item[x] in lst:



Hey andrewsw -

probably not enough sleep is the problem-

having understood that item in this case -is just a source for a sequence of numbers 0 1 2 etc-
so question #1 was really just me not understanding what the code was doing initially.

had never saw someone using a for loop to call list indexes in that way-

i've used enumerate in a for loop to get a 'listy' order of things-
but never for iterating both things at once- say like in a dictionary with -
for k,v in my_dict.items():
...


was only instance of exposure where two things are iterated simultaneously-

question #2 was from not realizing where the data is being drawn from-
its being drawn from lst- not from the ending of the declaration of the for loop
in the previous code- it was iterating through a range of numbers and not the list itself-

question #3 is probably got a lot to do with working within looping through nested lists-
but i havent read up on that topic enough to merit structuring a question.

ok, so having seen what i did wrong really helped- feel kind of foolish after realizing this from posting but
i guess thats ok too :)

Thank andrewsw will look at that codeacademy link

thats a pretty slick explain for enumerate*
Was This Post Helpful? 0
  • +
  • -

#5 maglev   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 26-September 15

Re: calling list indices within a for loop?

Posted 22 February 2016 - 02:48 PM

baavgai and andrewsw-

i think i might have sent my post before i was finished writing it^ could i possibly kill that?


yeah i was trying to actually iterate through a list to see if it was incrementing the proper way-

so broken lists [1,2,4,5,7,8,9] would return as [1,2,3,4,5,6,7,8,9]

and expand from there.
my earlier code had enumerate and calling the tuple indexes to write up a new list but it got really confusing really fast for me.

*zip() is a pretty cool idea forgot about that- python noob here.

on another note-not that i should be using a list comprehension i tried to chunk it into one, and im getting that whole
"returns a list of none's " thing since i guess you cant insert within a list comprehension- anyway i tried doing this too just to see
if i could do it as an 'all in one hehe'


lst = [1,2,3,5,6,8,9]
z = [ lst.insert(1, 2) for item in range( len(lst) - 1 if lst[item+1] - lst[item] == 1 ]



the code in its current state also breaks if the difference is greater than one [1,2,5,6,7,8] or [1,2,3,4,12]
and on negative numbers-

so many problems man!
Was This Post Helpful? 0
  • +
  • -

#6 maglev   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 26-September 15

Re: calling list indices within a for loop?

Posted 23 February 2016 - 04:23 PM

ok so new day fresh eyes got my code to work all right- thanks for your help and insights!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1