0
list1 = []
list.append([item1[i], item2[i], item3[i] for i in range(2)])

i.e how to populate list1 with [[item1[0], item2[0], item3[0]],[item1[1], item2[1], item3[1]]] through list comprehension where item1, item2 item3 are different lists? Example:

item1 = [1,2,3,4]
item2 = [1,4,9,16]
item3 = [1,8,27,64]

# I want list1 =
[[1,1,1],
[2,4,8],
[3,9,27],
[4,16,64]]
# through list comprehension AND APPEND STATEMENT
5
  • list(zip(item1, item2, item3)) ? Commented Sep 30, 2018 at 8:42
  • 1
    can you specify input and output? Commented Sep 30, 2018 at 8:51
  • Could you please provide a Minimal, Complete, and Verifiable example. Commented Sep 30, 2018 at 8:54
  • Added the example Commented Sep 30, 2018 at 9:24
  • According to me you can simply do this using :: a = [item1, item2, item3] Commented Sep 30, 2018 at 9:43

3 Answers 3

4

Try this:

list1 = list(zip(item1, item2, item3))

this will work until the minimum length of item1, item2, item3. If you want to go longer use itertools.zip_longest instead of zip.

example with zip:

item1 = [0,1]
item3 = [2,3]
item2 = [4,5,6]
list1 = list(zip(item1, item2, item3))

[(0, 4, 2), (1, 5, 3)]

example with itertools.zip_longest:

from itertools import zip_longest

item1 = [0,1]
item3 = [2,3]
item2 = [4,5,6]
list1 = list(zip_longest(item1, item2, item3))

[(0, 4, 2), (1, 5, 3), (None, 6, None)]
Sign up to request clarification or add additional context in comments.

Comments

3

Add another set of []'s to your code:

list1 = []
list1.append([[item1[i], item2[i], item3[i]] for i in range(len(item1))])

Note that this assumes that item1, item2 and item3 are all the same length

Alternately, for an exact match of your expected output, use the following:

list1 = [[item1[i], item2[i], item3[i]] for i in range(len(item1))]

Example data and output

item1 = [1, 2, 3]
item2 = ["A", "B", "C"]
item3 = [0.1, 0.2, 0.3]
list1 = [[item1[i], item2[i], item3[i]] for i in range(len(item1))]
print(list1)

>>> [[1, 'A', 0.1], [2, 'B', 0.2], [3, 'C', 0.3]]

Using .append(), the list comprehension becomes:

for i in range(len(item1)):
    list1.append([item1[i], item2[i], item3[i]])

However, if you want to use a list comprehension and still append the newly created lists onto list1, use += rather than .append():

list1 += [[item1[i], item2[i], item3[i]] for i in range(len(item1))]

.append() adds the given item onto the end of the list. += will instead add each sublist individually.

3 Comments

I also need to use append statement as there are already few rows in the list 1
Edited my answer to meet the requirements.
It does the exact job. But with just append statement and list comprehension, this task doesn't seem possible (without extra for loop)
0

If on python 2 just do zip without list:

zip(item1, item2, item3)

Demo:

item1 = [1, 2, 3]
item2 = [4,5,6]
item3 = [7,8,9]
print(zip(item1, item2, item3))

Output:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.