Assuming you have two lists of equal length, you can use the built-in zip()
function to iterate through both lists simultaneously, then append each item to a new list twice:
list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] duplicated_list = [] for item1, item2 in zip(list1, list2): duplicated_list.append(item1) duplicated_list.append(item2) print(duplicated_list)
Output:
[1, 'a', 2, 'b', 3, 'c', 4, 'd']
In this example, we create two lists list1
and list2
, then iterate over both of them using zip()
. In each iteration, we append the current value from list1
and list2
to the duplicated_list
twice. Finally, we print duplicated_list
which contains all the items from both original lists duplicated.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-12-03 11:00:00 +0000
Seen: 16 times
Last updated: Mar 27 '23
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?