Appending the Same String to a List of Strings in Python

Appending the same string to a list of strings in Python

The simplest way to do this is with a list comprehension:

[s + mystring for s in mylist]

Notice that I avoided using builtin names like list because that shadows or hides the builtin names, which is very much not good.

Also, if you do not actually need a list, but just need an iterator, a generator expression can be more efficient (although it does not likely matter on short lists):

(s + mystring for s in mylist)

These are very powerful, flexible, and concise. Every good python programmer should learn to wield them.

Append different strings to a list of strings depending on its position in the list

The problem here is list comprehension iterates over an entire iterable and creates a new list, you can add conditions to the value but just iterating might in fact be easier to understand for people, also one more thing to note is strings are immutable i.e. when you are saying modify you are in fact creating a new string. Try this:

configurations_new = configurations_v1.copy()
for index, value in enumerate(configurations_v1):
if index < 75:
configurations_new[index] = configurations_new[index] + '1'
elif index < 150:
configurations_new[index] = configurations_new[index] + '2'
else:
configurations_new[index] = configurations_new[index] + '3'

Which when written using list comprehension is:

configurations_new = [i + '1' if index < 75 else i + '2' if index < 150 else i + '3' for index, i in enumerate(configurations_v1)]

Concatenate string to the end of all elements of a list in python

rebuild the list in a list comprehension and use str.format on both parameters

>>> string="a"
>>> List1 = [ 1 , 2 , 3 ]
>>> output = ["{}{}".format(i,string) for i in List1]
>>> output
['1a', '2a', '3a']

append same string to list of strings in a column pandas

We can use explode to unnest your list, then add the strings together and finally use groupby on the index and use agg(list) to get your list back:

ex = df.explode('c')
ex['c'] = ex['c'] + ex['a']

df['c'] = ex.groupby(ex.index)['c'].agg(list)
   a  b         c
0 d 1 [fd, hd]
1 f 2 [uf, vf]
2 g 3 [ig, og]

Add the same word in each string within a list

This simple list comprehension should help you:

my_list = ["house","table","coffe","door"]

my_list = [elem+" word" for elem in my_list]

print(my_list)

Output:

['house word', 'table word', 'coffe word', 'door word']

How do you add a string to every string inside of a list?

When you are iterating over the nums list, i is each value in the list; on the first iteration, i is equal to one, and on the second i is equal to two.

So when you are trying to access nums[i] you are trying to access nums["one"] (on the first iteration), which obviously doesn't exist.

To solve this, you can either change the for loop to an index based one, using range:

for i in range(len(nums)):
nums2.append(nums[i] + ' thousand')

Or you could just stop trying to access the list from within the loop altogether, and use the value of i as the prefix to append thousand to:

for i in nums:
nums2.append(i + ' thousand')

appending a string to a list of strings if the string in the list is equal to some variable in python

You could use a list comprehension:

>>> a = ['item1', 'item2', 'item3']
>>> b = 'item2'
>>> c = [f'*{s}*' if s == b else s for s in a]
>>> c
['item1', '*item2*', 'item3']

How to increment the strings in list with digits if the element already present in the list

You can use a dictionary to keep track of the number of occurrences of a string seen so far. You could use a list comprehension, but the only way I could think of would involve list slicing and .count() or something super hacky, both of which would be less desirable than the one-pass implementation below:

a = ['abc', 'abc', 'h', 'xv', 'xv', 'xv', 'h', 'h', 'h', 'h']
result = []
occurrences = {}

for elem in a:
if elem in occurrences:
elem_to_add = elem + str(occurrences[elem])
occurrences[elem] += 1
else:
elem_to_add = elem
occurrences[elem] = 1
result.append(elem_to_add)

print(result)


Related Topics



Leave a reply



Submit