Adding the same string to a string list in Python

I am trying to take one line and add it to each line contained in the list, and then add a new list with completed lines. Example:

list = ['foo', 'fob', 'faz', 'funk'] string = 'bar' *magic* list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar'] 

I tried using loops and trying to figure out a list, but it was rubbish. As always, any help is greatly appreciated.

+143
python list
Jan 12 '10 at 16:50
source share
11 answers

The easiest way to do this is with a list:

 [s + mystring for s in mylist] 

Please note that I avoided using inline names such as list because it shadows or hides inline names, which is not very good.

Also, if you really don't need a list, but just need an iterator, the generator expression may be more efficient (although it is unlikely in short lists):

 (s + mystring for s in mylist) 

They are very powerful, flexible and concise. Every good Python programmer needs to learn how to own them.

+240
Jan 12 '10 at 16:51
source share
 my_list = ['foo', 'fob', 'faz', 'funk'] string = 'bar' my_new_list = [x + string for x in my_list] print my_new_list 

This will print:

 ['foobar', 'fobbar', 'fazbar', 'funkbar'] 
+20
Jan 12 '10 at 16:59
source share

I have not found a way to comment on the answers so far. So there it is. I support the answer of Ignacio Vasquez-Abrams list2 = ['%sbar' % x for x in list] .

Other answers with [string + "bar" for string in list] will work in most cases, but if you make a more general decision for the simplest case - IMHO - following Python design principles. There should preferably be one obvious way to do this. %sbar running all the time.

+2
Jan 12 '10 at 19:57
source share

map seems to be the right tool for the job.

 my_list = ['foo', 'fob', 'faz', 'funk'] string = 'bar' list2 = map(lambda orig_string: orig_string + string, my_list) 

For more information on map see this section on functional programming tools.

+2
07 Oct '13 at
source share

Performing the following experiment, the pythonic path:

 [s + mystring for s in mylist] 

seems ~ 35% faster than the obvious use of the for loop as follows:

 i = 0 for s in mylist: mylist[i] = s+mystring i = i + 1 

Experiment

 import random import string import time mystring = '/test/' l = [] ref_list = [] for i in xrange( 10**6 ): ref_list.append( ''.join(random.choice(string.ascii_lowercase) for i in range(10)) ) for numOfElements in [5, 10, 15 ]: l = ref_list*numOfElements print 'Number of elements:', len(l) l1 = list( l ) l2 = list( l ) # Method A start_time = time.time() l2 = [s + mystring for s in l2] stop_time = time.time() dt1 = stop_time - start_time del l2 #~ print "Method A: %s seconds" % (dt1) # Method B start_time = time.time() i = 0 for s in l1: l1[i] = s+mystring i = i + 1 stop_time = time.time() dt0 = stop_time - start_time del l1 del l #~ print "Method B: %s seconds" % (dt0) print 'Method A is %.1f%% faster than Method B' % ((1 - dt1/dt0)*100) 

results

 Number of elements: 5000000 Method A is 38.4% faster than Method B Number of elements: 10000000 Method A is 33.8% faster than Method B Number of elements: 15000000 Method A is 35.5% faster than Method B 
+2
Feb 15 '17 at 13:54 on
source share

Extend a bit to "Add a list of lines to a list of lines":

  import numpy as np lst1 = ['a','b','c','d','e'] lst2 = ['1','2','3','4','5'] at = np.full(fill_value='@',shape=len(lst1),dtype=object) #optional third list result = np.array(lst1,dtype=object)+at+np.array(lst2,dtype=object) 

Result:

 array(['a@1', 'b@2', 'c@3', 'd@4', 'e@5'], dtype=object) 

The dtype object can be further converted

+2
Aug 14 '18 at 9:30
source share

you can use lambda inside the map in python. wrote a gray code generator. https://github.com/rdm750/rdm750.imtqy.com/blob/master/python/gray_code_generator.py # your code goes here "" n-1-bit code, with 0 added to each word, followed by n -1-bit code in reverse order, with 1 appended to each word. '' '

  def graycode(n): if n==1: return ['0','1'] else: nbit=map(lambda x:'0'+x,graycode(n-1))+map(lambda x:'1'+x,graycode(n-1)[::-1]) return nbit for i in xrange(1,7): print map(int,graycode(i)) 
+1
Jun 16 '16 at 15:15
source share
 list2 = ['%sbar' % (x,) for x in list] 

And do not use list as a name; it obscures the built-in type.

0
Jan 12
source share
 new_list = [word_in_list + end_string for word_in_list in old_list] 

Using names such as a β€œlist” for your variable names is bad, as it will overwrite / override built-in functions.

0
Jan 12 '10 at 16:57
source share

Here is a simple answer using pandas .

 import pandas as pd list = ['foo', 'fob', 'faz', 'funk'] string = 'bar' list2 = (pd.Series(list) + string).tolist() list2 # ['foobar', 'fobbar', 'fazbar', 'funkbar'] 
0
Apr 25 '19 at 4:52
source share

So just in case

 list = ['foo', 'fob', 'faz', 'funk'] string = 'bar' for i in range(len(list)): list[i] += string print(list) 
0
Jul 11 '19 at 9:48
source share



All Articles