Article 541S3 combine list in python3

combine list in python3

by
sharky
from LinuxQuestions.org on (#541S3)
This is not a typical combination.

Start with two list of different length were the second list is always double the length of the first list.

Example:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]

The result I am looking for is:
listR = ["a", 0, "a", 1 "b", 2, "b", 3 "c", 4, "c", 5 "d", 6, "d", 7]

I got this to work in python2:

Code:list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]
listR = []

for index2 in range(len(list2)):
index1 = int(round(index2/2))
listR.append(list1[index1])
listR.append(list2[index2])

print(listR)The result of the print statement is exactly what I'm looking for. But in python3 the index for list1 goes out of range because the rounding is different.

If I print just the index1 in python2 I see 0 0 1 1 2 2 3 3 but in python3 I get 0 0 1 2 2 2 3 4.

There are ways to work around this to accomplish my goal but besides the rounding issue I don't like the brute force nature of the code as is and was wondering if a more elegant python solution was available.

Thanks to all in advance.latest?d=yIl2AUoC8zA latest?i=HjtA6Z71QRw:4Y7hYbTaKJY:F7zBnMy latest?i=HjtA6Z71QRw:4Y7hYbTaKJY:V_sGLiP latest?d=qj6IDK7rITs latest?i=HjtA6Z71QRw:4Y7hYbTaKJY:gIN9vFwHjtA6Z71QRw
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments