Append List to a List

Appending a list to a list of lists

First, you are trying to extend a list, not append to it. Specifically, you want to do

b[2].extend(a)

append() adds a single element to a list. extend() adds many elements to a list. extend() accepts any iterable object, not just lists. But it's most common to pass it a list.

Once you have your desired list-of-lists, e.g.

[[4], [3], [8, 5, 4]] 

then you need to concatenate those lists to get a flat list of ints. You can use sum() for that -- adding lists is not that different from adding ints.

b = sum(b, [])

The trick here is that you have to pass the initial (empty) value to sum(), otherwise it tries to add the lists in b as though they were numbers.

Finally, you can sum the flattened list as you intended:

sum(b)

How to append a list to another list in a python for loop when each element of the existing list is being changed inside the for loop?

When you append r1 twice to r2 it essentially makes r2 a list of [r1, r1] not the contents of r1 when it was appended, so when r1 is changed before the second append, the first element in r2 which is a reference to r1 is also changed.

One solution is to not use r1 at all and just append the contents directly:

r2 = []
r_1 = {'G':32,'H':3}
for i in r_1:
r2.append([i, i+"I"])
print(r2)

A second solution is to append a copy of r1 to avoid the two elements having the same reference:

r2 = []
r_1 = {'G':32,'H':3}
r1 = [None, None]
for i in r_1:
r1[0] = i
r1[1] = i + "I"
r2.append(r1.copy())
print(r2)

What is the difference between Python's list methods append and extend?

append appends a specified object at the end of the list:

>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]

extend extends the list by appending elements from the specified iterable:

>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]

Python : append a list to a list

append can only add a single value. I think what you may be thinking of is the extend method (or the += operator)

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
list_first_3.extend(list1[:3])

assert list_first_3 == ["cat", 3.14, "dog"]

or

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
list_first_3 += list1[:3]

assert list_first_3 == ["cat", 3.14, "dog"]

otherwise you'll need a loop:

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
for value in list1[:3]: list_first_3.append(value)

assert list_first_3 == ["cat", 3.14, "dog"]

with append but without a loop would be possible using a little map() trickery:

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
any(map(list_first_3.append,list1[:3]))

assert list_first_3 == ["cat", 3.14, "dog"]

Appending an integer and a list to a list of lists as a single list

This is one simple way to do it, as long as the number of lists and integers you are planning to append is not large,

a = 4
b = [3,1]
b2 = [5,6,7]
b3 = [2]

b.insert(0,a)
c = []

c.append([x for x in b])
c[0] += b2
c[0] += b3

print(c)

Here I extended your example to cover two additional list. The code should print:

[[4, 3, 1, 5, 6, 7, 2]]

Basically, you prepend the integer to the first list, b. Then you use a list comprehension to create a sublist out of the new b in c. Finally, you simply concatenate all the other lists to it with +=. c[0] is the location of the inner - target list.

This method is not very efficient, and is pretty "manual", but again, it seems suitable for small number of lists with not too many elements.

Append two individual lists to list of lists

The two actions you are describing are distinctly different. The first is creating the outer list (a.k.a. c) and the second is appending to it.

To make the process more uniform you can just start off with an outer list and append all the child lists to it.

c = []
a=[1,2,3]
b=[4,5,6]
d=[7,8,9]

c.append(a)
c.append(b)
c.append(d)

c is now

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

How to append all elements of one list to another one?

You could use addition:

>>> a=[5, 'str1']
>>> b=[8, 'str2'] + a
>>> b
[8, 'str2', 5, 'str1']

how to append a list of of lists with different lengths

IIUC, you want to add each of the first, second ... nth elements of each sublist as rows of the data frame, which is equivalent to the dataframe of the transpose of the list of lists.

You don't need a for loop to do this in python.

Using zip with unpacking operator

You can do it in a few ways but the one way would be zip with unpacking operator * using list(zip(*l))

l = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]

lt = pd.DataFrame(zip(*l)) #<---
print(lt)
   0  1  2   3   4
0 1 4 7 10 13
1 2 5 8 11 14
2 3 6 9 12 15

Using pandas transpose

A simpler way would be to use pandas to do this where you can simply use transpose -

l = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]

lt = pd.DataFrame(l).T #<---
print(lt)
   0  1  2   3   4
0 1 4 7 10 13
1 2 5 8 11 14
2 3 6 9 12 15


Related Topics



Leave a reply



Submit