Tagged Questions
55
votes
8answers
48k views
In Python, what is the difference between “.append()” and “+= []”?
What is the difference between:
some_list1 = []
some_list1.append("something")
and
some_list2 = []
some_list2 += ["something"]
55
votes
6answers
35k views
How can I get the concatenation of two lists in Python without modifying either one?
In Python, the only way I can find to concatenate two lists is list.extend, which modifies the first list. Is there any concatenation function that returns its result without modifying its arguments?
30
votes
4answers
36k views
String and integer concatenation
I want to create string using integer appended to it, in a for loop.
# Create string0, string1 ..... string10
for i in range [1,10]:
string="string"+i
But It has returned an error because i is not ...
20
votes
5answers
16k views
how to concatenate two dictionaries to create a new one in Python?
Say I have three dicts
d1={1:2,3:4}
d2={5:6,7:9}
d3={10:8,13:22}
How do I create a new d4 that combines these three dictionaries? i.e.:
d4={1:2,3:4,5:6,7:9,10:8,13:22}
13
votes
2answers
9k views
Concatenating two one-dimensional numpy arrays
I have two simply one-dimensional arrays in numpy. I should be able to concatenate them using numpy.concatenate. But this is what I get
import numpy
a = numpy.array([1,2,3])
b = numpy.array([5,6])
...
11
votes
6answers
3k views
Most Pythonic way to concatenate strings
Given this harmless little list:
>>> lst = ['o','s','s','a','m','a']
My goal is to pythonically concatenate the little devils using one of the following ways:
A. plain ol' string function ...
11
votes
1answer
153 views
List += Tuple vs List = List + Tuple
Let's say I have these assignments:
points = []
point = (1, 2)
How come when I do this:
points += point
It works completely fine, and gives me points = [1, 2].
However, If I do something like:
...
9
votes
4answers
959 views
how to concatenate multiple files for stdin of Popen
I'm porting a bash script to python 2.6, and want to replace some code:
cat $( ls -tr xyz_`date +%F`_*.log ) | filter args > bzip2
I guess I want something similar to the "Replacing shell pipe ...
6
votes
6answers
5k views
Python: Cut of the last word of a sentence?
I need to cut out the last word of a block of text. The reason is i'm grabbing some text from a database and i don't know if the last word will be cut off or not, so to be safe i'm getting rid of the ...
6
votes
6answers
378 views
Why does not the + operator change a list while .append() does?
I'm working through Udacity and Dave Evans introduced an exercise about list properties
list1 = [1,2,3,4]
list2 = [1,2,3,4]
list1=list1+[6]
print(list1)
list2.append(6)
print(list2)
list1 = ...
6
votes
3answers
229 views
Speed vs security vs compatibility over methods to do string concatenation in Python
Similar questions have been brought (good speed comparison there) on this same subject. Hopefully this question is different and updated to Python 2.6 and 3.0.
So far I believe the faster and most ...
6
votes
2answers
883 views
Is there an efficient way of concatenating scipy.sparse matrices?
I'm working with some rather large sparse matrices (from 5000x5000 to 20000x20000) and need to find an efficient way to concatenate matrices in a flexible way in order to construct a stochastic matrix ...
5
votes
6answers
467 views
What is the most efficient way to concatenate two strings and remove everything before the first ',' in Python?
In Python, I have a string which is a comma separated list of values. e.g. '5,2,7,8,3,4'
I need to add a new value onto the end and remove the first value,
e.g.
'5,22,7,814,3,4' -> ...
5
votes
4answers
16k views
Python String Concatenation - concatenating '\n'
I am new to Python and need help trying to understand two problems i am getting relating to concatenating strings. I am aware that strings can be added to concatenate each other using + symbol like ...
4
votes
2answers
7k views
TypeError: cannot concatenate 'str' and 'int' objects
I'm learning Python now, yay! Anyway, I have small problem. I don't see problem in here:
x = 3
y = 7
z = 2
print "I told to the Python, that the first variable is %d!" % x
print "Anyway, 2nd and 3rd ...