Linked Questions
189 questions linked to/from What is the difference between Python's list methods append and extend?
240 votes
6 answers
351k views
What is the syntax to insert one list into another list in python? [duplicate]
Given two lists: x = [1,2,3] y = [4,5,6] What is the syntax to: Insert x into y such that y now looks like [1, 2, 3, [4, 5, 6]]? Insert all the items of x into y such that y now looks like [1, 2, 3, ...
152 votes
7 answers
195k views
Python append() vs. += operator on lists, why do these give different results?
Why do these two operations give different results? >>> c = [1, 2, 3] >>> c [1, 2, 3] >>> c += c >>> c [1, 2, 3, 1, 2, 3] >>> c = [1, 2, 3] >>> c....
16 votes
6 answers
76k views
How to create tuple with a loop in python [duplicate]
I want to create this tuple: a=(1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9) I tried with this a=1,1,1 for i in range (2,10): a=a,(i,i,i) However it creates a tuple ...
7 votes
2 answers
14k views
Appending to a Python list without making list of lists [duplicate]
I start out with an empty list and prompt the user for a phrase. I want to add each character as a single element of the array, but the way I'm doing this creates a list of lists. myList = [] for i ...
10 votes
6 answers
5k views
Append tuples to a tuples [duplicate]
I can do append value to a tuples >>> x = (1,2,3,4,5) >>> x += (8,9) >>> x (1, 2, 3, 4, 5, 8, 9) But how can i append a tuples to a tuples >>> x = ((1,2), (3,4), (...
33 votes
2 answers
6k views
How to merge two dictionaries with same key names [duplicate]
I am new to Python and am trying to write a function that will merge two dictionary objects in python. For instance dict1 = {'a':[1], 'b':[2]} dict2 = {'b':[3], 'c':[4]} I need to produce a new ...
3 votes
1 answer
8k views
Is a Python list's += operator equivalent to append() or extend()? [duplicate]
Python lists have a += operator as well as append and extend methods. If l is a list, is l += ... equivalent to l.append(...), l.extend(...), both, or neither?
2 votes
1 answer
12k views
'int' object is not iterable while using "list.extend" [duplicate]
Hi could someone help with this code,i am getting the error: " 'int' object is not iterable " at line 28(A.extend(n)) since i am new to python i am unable to figure out a solution any help is ...
-1 votes
2 answers
2k views
Flatten list of Tuples in Python [duplicate]
I am using a recursive function to create a flow path through a maze. The function returns the correct path tuples (row,col), but I need it in the form of a List of tuples. For example I need to ...
2 votes
3 answers
799 views
How to repeat list's elements with each different n times and in Python? [duplicate]
The idea is to repeat list's elements with each different n times as below. ls = [7, 3, 11, 5, 2, 3, 4, 4, 2, 3] id_list_fname = ['S11', 'S15', 'S16', 'S17', 'S19', 'S3', 'S4', 'S5', 'S6', 'S9'] ...
3 votes
1 answer
3k views
Python list .extend method breaks string into characters [duplicate]
I am reading two columns of values out of a .csv file a line at a time, which yields a list of row entries made up of two strings (something like ['cat1', 'dog1']) When I iterate over all the rows I ...
1 vote
1 answer
5k views
python store string to array not characters [duplicate]
I'm new to python. I tried to store bunch of strings to an array and at the end print out the array, however it print out as a long list of characters. here is my code: user_with_no_records = [""] ...
3 votes
1 answer
4k views
python list: append vs += [duplicate]
For Python list, is append() the same as +=? I know that + will lead to the creation of a new list, while append() just append new stuff to the old list. But will += be optimized to be more similar to ...
-3 votes
3 answers
3k views
Print list inside a list in python [duplicate]
values = [] records = contains two records of different person values.extend((records.name, records.age , records.gender)) print values This will print [some_name,age,gender , ...
0 votes
2 answers
546 views
Iterate through a list and split into a 1d list [duplicate]
Im trying to loop through a list in python and split words based on characters. I want to return a 1 dimension list as the result. Example wordlist = ['border\collie', 'dog\cat', 'horse\hound'] # ...