in Education by
I have list1 of a certain length and list2 of a certain length. Let's say: list1 = [32, 345, 76, 54] list2 = [43, 65, 76, 23, 12, 23, 44] I need to make list1 loop back around until it is the same length as list2. Or, if list1 is longer, I need it to cut down to the length of list2. For the example above, I am looking to make: list1 = [32, 345, 76, 54, 32, 345, 76] It doesn't necessarily have to remain list1. It can be a new list I just need the same values from list1 looped back around a certain number of times. How do I go about doing this? I'm fairly new to python and I haven't been able to find anything that works. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Get to know the wonderful itertools module! from itertools import cycle, islice result = list(islice(cycle(list1), len(list2))) If all you need is to iterate both list "together", this is even easier: for x, y in zip(cycle(list1), list2): print(x, y)

Related questions

0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    The data are kept in a list whose elements are dictionaries, where each dictionary contains the data for a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    How can I return the list in alphabets? I have sequence translator, and a python code that reads both ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    What will be the output of the following Python code? >>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2) a) [1, 4] b) [1, 3, 4] c) [4, 3] d) [1, 3]...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Consider a list2 with values 70,60,50. What will be the output statements considering that the above object has ... print(list2*3) Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    What is the output of print list1 + list2, if list1 = [ 'abcd', 786 , 2.23, 'john', 70.2 ] and ist2 = [123, 'john']?...
asked Nov 23, 2020 in Technology by JackTerrance
0 votes
    How will you get the length of a list in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    A data frame is a special type of list where every element of the list has ______ length. (a) Same (b) ... and Out of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    The length of a list is ______ to the number of components in that list. (a) Double (b) Equal (c) Triple ... and Out of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I'd like to line up items approximately like this: item1 item2 i3 longitemname i4 longitemname2 anotheritem ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'd like to line up items approximately like this: item1 item2 i3 longitemname i4 longitemname2 anotheritem ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'd like to line up items approximately like this: item1 item2 i3 longitemname i4 longitemname2 anotheritem ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'd like to line up items approximately like this: item1 item2 i3 longitemname i4 longitemname2 anotheritem ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    A train is moving at a speed of 132 km/hr. If the length of the train is 110 metres, how long will it take to cross a railway ... A. 7½ sec B. 10 sec C. 12 ½ sec D. 15 sec...
asked Feb 19, 2021 in Education by JackTerrance
0 votes
    How will you get the length of the string?...
asked Nov 25, 2020 in Technology by JackTerrance
...