Let's say I have a list:
list = ["word", "word2", "word3"]
and I want to change this list to:
list = ["word:", "word2:", "word3:"]
is there a quick way to do this?
Let's say I have a list:
list = ["word", "word2", "word3"]
and I want to change this list to:
list = ["word:", "word2:", "word3:"]
is there a quick way to do this?
List comprehensions to the rescue!
list = [item + ':' for item in list]
In a list of
['word1', 'word2', 'word3']
This will result in
['word1:', 'word2:', 'word3:']
You can read more about them here.
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
You can use a list comprehension as others have suggesed. You can also use this code:
newlist = map(lambda x: x+':', list)
simple question but though a noob friendly answer :)
list = ["word", "word2", "word3"]
n = len(list)
for i in range(n):
list[i] = list[i] + ':'
print list
strings can be concatenated using '+' in python and and using 'len' function you can find the length of list. iterate i using for loop till n and concatenate ':' to the list eliments. feel free to post but thing of google first :P