Python Forum
Accessing data in zip - Index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing data in zip - Index out of range
#1
i have 3 lists
Quote:Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
Years=[17,20,26,40,67,88,96,99,37]
Order=[1,2,3,4,5,6,7,8,9]

i am trying to extract data from it as follows
Jack Jack
Jack Jeni
Jack Monsa
Jack Mehus
Jack Kuis
Jack Tim
Jack Tony
Jack Yestgf
Jack Pere
Jeni Jeni
Jeni Monsa
Jeni Mehus
Jeni Kuis
Jeni Tim
Jeni Tony
Jeni Yestgf
Jeni Pere
Jeni Jack

and so on, for this i am writing a code as below
Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
Years=[17,20,26,40,67,88,96,99,37]
Order=[1,2,3,4,5,6,7,8,9]
for names,years,orders in zip(Names,Years,Order):
   for i in range(1,(len(Order)+1)):
       for j in range(i,i+len(Order)):
           jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j]
           print(names,Names[j])
i am getting following output
Quote:Traceback (most recent call last):
Jack Jeni
File "C:/Users/Administrator/PycharmProjects/Tesrt/forumsquestion.py", line 7, in <module>
Jack Monsa
Jack Mehus
Jack Kuis
Jack Tim
Jack Tony
Jack Yestgf
Jack Pere
jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j]
IndexError: list index out of range

Process finished with exit code 1

It looks like i am doing everything wrong Cry any help would be appreciated
Reply
#2
I don't understand why the order and years lists are there,
Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
# Years=[17,20,26,40,67,88,96,99,37]
# Order=[1,2,3,4,5,6,7,8,9]
# for names,years,orders in zip(Names,Years,Order):
#    for i in range(1,(len(Order)+1)):
#        for j in range(i,i+len(Order)):
#            jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j]
#            print(names,Names[j])

for name in Names:
    for name1 in Names:
        print('{} {}'.format(name, name1))
Reply
#3
or you can use itertools.product

from itertools import product
names=["Jack", "Jeni", "Monsa", "Mehus", "Kuis", "Tim", "Tony", "Yestgf", "Pere"]
for two_names in product(names, names):
    print(' '.join(two_names))
Reply
#4
(Mar-03-2018, 06:57 PM)Larz60+ Wrote: I don't understand why the order and years lists are there,
Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
# Years=[17,20,26,40,67,88,96,99,37]
# Order=[1,2,3,4,5,6,7,8,9]
# for names,years,orders in zip(Names,Years,Order):
#    for i in range(1,(len(Order)+1)):
#        for j in range(i,i+len(Order)):
#            jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j]
#            print(names,Names[j])

for name in Names:
    for name1 in Names:
        print('{} {}'.format(name, name1))
i actually want this to print in an order, it starts from Jack Jack and then 2nd set starts from Jeni Jeni, that is why i have used the order list
Reply
#5
did you run what I gave you? Try it!
Reply
#6
(Mar-04-2018, 01:05 AM)Larz60+ Wrote: did you run what I gave you? Try it!
Yes, it didnt give me the output i was looking for Sad part of that is as follows
Quote:Jack Jack
Jack Jeni
Jack Monsa
Jack Mehus
Jack Kuis
Jack Tim
Jack Tony
Jack Yestgf
Jack Pere
Jeni Jack
Jeni Jeni


See where the 2nd set starts :(
Reply
#7
Simpler is better:
names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
for i, a in enumerate(names):
    for b in names[i:] + names[:i]:
        print(a, b)
Reply
#8
(Mar-04-2018, 06:41 AM)Gribouillis Wrote: Simpler is better:
names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
for i, a in enumerate(names):
    for b in names[i:] + names[:i]:
        print(a, b)
it starts to malfunction if we go a bit deeper
names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
for i, a in enumerate(names):
    for b in names[i:] + names[:i]:
        for c in names[i:] + names[:i]:
            for d in names[i:] + names[:i]:
                print(a, b, c, d)
Quote:Jack Jack Jack Jack
Jack Jack Jack Jeni
Jack Jack Jack Monsa
Jack Jack Jack Mehus
Jack Jack Jack Kuis
Jack Jack Jack Tim
Jack Jack Jack Tony
Jack Jack Jack Yestgf
Jack Jack Jack Pere
Jack Jack Jeni Jack
Jack Jack Jeni Jeni
Reply
#9
if I get right your ever-changing goal, you just need to have similar construct with enumerate for every loop but the last one
Reply
#10
(Mar-04-2018, 07:34 AM)buran Wrote: if I get right your ever-changing goal, you just need to have similar construct with enumerate for every loop but the last one

:( sorry, i dont get you ,can you please show me the code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,858 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,325 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  List index out of range when turning CSV into dict ranbarr 15 6,401 May-12-2021, 10:38 AM
Last Post: ranbarr
  list index out of range mcgrim 2 2,899 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 3,842 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,511 Aug-17-2018, 11:18 PM
Last Post: felie04
  "List index out of range" for output values pegn305 3 5,292 Nov-26-2017, 02:20 PM
Last Post: heiner55
  list index out of range DrPengin 1 3,682 Nov-09-2017, 08:35 PM
Last Post: gruntfutuk
  string index out of range cusick11 9 15,082 Mar-03-2017, 11:45 PM
Last Post: ichabod801
  Invalid syntax: string index out of range darkreaper1959 6 6,141 Jan-22-2017, 06:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020