Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting list values from a list to another in a specific order in Python

Tags:

python

list

I was trying to insert list values from one list to another, but in a specific order, where dates[0] entered text[1], dates[1] entered text[3] and so on.

dates=['21/11/2044', '31/12/2018', '23/9/3000', '25/12/2007']

text=['What are dates? ', ', is an example.\n', ', is another format as
well.\n', ', also exists, but is a bit ludicrous\n', ', are examples but more commonly used']

I tried this method:

for j in range(len(text)):
  for i in range(len(dates)):
   text.insert(int((j*2)+1), dates[i])

This was the result, which was incorrect:

['What are dates? ', '25/12/2007', '23/9/3000', '25/12/2007', '23/9/3000',
'25/12/2007', '23/9/3000', '25/12/2007', '23/9/3000', '25/12/2007',
'23/9/3000', '31/12/2018', '21/11/2044', '31/12/2018', '21/11/2044',
'31/12/2018', '21/11/2044', '31/12/2018', '21/11/2044', '31/12/2018',
'21/11/2044', ', is an example.\n', ', is another format as well.\n', ',
also exists, but is a bit ludicrous\n', ', are examples but more commonly used']

I was trying to get back a list that reads like:

['What are dates? ','21/11/2044', 'is an example.\n','31/12/2018', ', is
another format as well.\n','23/9/3000', ', also exists, but is a bit
ludicrous\n', '25/12/2007',', are examples but more commonly used']

Is there a way to insert dates[i] into text[2*j+1] in the way I wanted? Should I even use a for loop, or is there another way without listing everything in dates as well?

like image 838
Alden Tan Avatar asked Jan 07 '18 14:01

Alden Tan


People also ask

How do I assign a list of values from one list to another?

When you copying a list to another list using = sign, both the list refer to the same list object in the memory. So modifying one list, other automatically changes as they both refer to same object. To copy a list use slice operator or copy module.

How do I add values from one list to another in Python?

Use the extend() Method to Append a List Into Another List in Python. Python has a built-in method for lists named extend() that accepts an iterable as a parameter and adds it into the last position of the current iterable. Using it for lists will append the list parameter after the last element of the main list.

Can you put a list in a list Python?

To add a list inside a list as an element, use the list. append() method. The list append() is a built-in Python function that adds a single element to the existing list.

How do you create a list from a different list in Python?

List copy using =(assignment operator) This is the simplest method of cloning a list by using = operators. This operator assigns the old list to the new list using Python = operators. Here we will create a list and then we will copy the old list into the new list using assignment operators.


1 Answers

A simpler way to achieve this is using itertools.zip_longest in Python 3.x (or izip_longest in Python 2.x) as:

>>> from itertools import zip_longest # for Python 3.x

>>> # For Python 2.x
>>> # from itertools import izip_longest

>>> dates=['21/11/2044', '31/12/2018', '23/9/3000', '25/12/2007']
>>> text=['What are dates? ', ', is an example.\n', ', is another format as well.\n', ', also exists, but is a bit ludicrous\n', ', are examples but more commonly used']

>>> [w for x in zip_longest(text, dates, fillvalue='') for w in x if w]
['What are dates? ', '21/11/2044', ', is an example.\n', '31/12/2018', ', is another format as well.\n', '23/9/3000', ', also exists, but is a bit ludicrous\n', '25/12/2007', ', are examples but more commonly used']

The issue with your code is that you have nested for loops, and that's why for each index of j, all values of dates are getting added.

like image 69
Moinuddin Quadri Avatar answered Sep 26 '22 03:09

Moinuddin Quadri