elements = ['%{0}%'.format(element) for element in elements]
Answer from JBernardo on Stack Overflowelements = ['%{0}%'.format(element) for element in elements]
You can use list comprehension:
elements = ["%" + e + "%" for e in elements]
How to modify list in python
How do I act on each element of a list while editing it?
python - Modifying each element in a list in a function - Stack Overflow
python - Modify each element in a list - Stack Overflow
I have my answer, thanks all!
I need a better way to do this:
for element in mainlist:
split(element)
mainlist.extend(temp)
def split(element):
global mainlist, temp
#Do the splitting (I am 100% this is working correctly)
temp = [split1,split2]
mainlist.remove(element)The program is recursive, processing each part down until it is finished. (Not shown here)
In my output there are elements that are being "forgotten" after the first pass.
I assume it probably ahs something to do with remove() and "for". How do I better implement this?
Your function can only return once, so you need to back it out of the for loop. As written, your function currently will return x after the first iteration, so none of the remaining elements are modified.
def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x
An alternative, by the way, is to use a simple list comprehension, this will not modify the original list and will create a new one that you can assign back to the original variable if you'd like
def double_list(x):
return [i*2 for i in x]
>>> n = [3, 5, 7]
>>> n = double_list(n)
>>> n
[6, 10, 14]
If you prefer to modify the actual list argument, you can use change the function to
def double_list(x):
for index, value in enumerate(x):
x[index] = 2 * value
>>> n = [3, 5, 7]
>>> double_list(n)
>>> n
[6, 10, 14]
you can do it very easy with list comprehension.
n = [3, 5, 7]
def double_list(x):
return [y*2 for y in x]
print double_list(n)
'[6, 10, 14]'
I have a list of strings
data = ['1.2','3.4','3.5']
and I want to convert all of them to floats what I'm doing now is
indexs = []
for i in data:
i = float(i)
indexs.append(i)is there a way to do it without making an entirely new list?
list = [1,0,1,0,1,1,0,0] print(list) for i in list:
if i == 0: i = 1
print(list)
I have a list and would like to for loop to change all of the 0s to 1s
but no changes happen
the if statement is catching the 0s but not changing them to 1s
idk why "i = 1" is not working
So I have as an exercise to create a hangman type of game, so I need to change all the elements in the list (the movie name, let's say) to a "_". How can I code it?
For the general case, use enumerate to get the index and the item. Then you can assign back to the list
for i, item in enumerate(exampleList):
if remap != item:
exampleList[i] = item + 1
remap = remap + 1
Why would you want to do this?
print range(len(your_list))
Otherwise, use a list-comp:
print [i+1 if some_criteria(i) else 0 for i in your_list]
You could do this:
for idx, item in enumerate(list):
if 'foo' in item:
item = replace_all(...)
list[idx] = item
You need to use the enumerate function: python docs
for place, item in enumerate(list):
if "foo" in item:
item = replace_all(item, replaceDictionary)
list[place] = item
print item
Also, it's a bad idea to use the word list as a variable, due to it being a reserved word in python.
Since you had problems with enumerate, an alternative from the itertools library:
for place, item in itertools.zip(itertools.count(0), list):
if "foo" in item:
item = replace_all(item, replaceDictionary)
list[place] = item
print item
Since the loop below only modifies elements already seen, it would be considered acceptable:
a = ['a',' b', 'c ', ' d ']
for i, s in enumerate(a):
a[i] = s.strip()
print(a) # -> ['a', 'b', 'c', 'd']
Which is different from:
a[:] = [s.strip() for s in a]
in that it doesn't require the creation of a temporary list and an assignment of it to replace the original, although it does require more indexing operations.
Caution: Although you can modify entries this way, you can't change the number of items in the list without risking the chance of encountering problems.
Here's an example of what I mean—deleting an entry messes-up the indexing from that point on:
b = ['a', ' b', 'c ', ' d ']
for i, s in enumerate(b):
if s.strip() != b[i]: # leading or trailing whitespace?
del b[i]
print(b) # -> ['a', 'c '] # WRONG!
(The result is wrong because it didn't delete all the items it should have.)
Update
Since this is a fairly popular answer, here's how to effectively delete entries "in-place" (even though that's not exactly the question):
b = ['a',' b', 'c ', ' d ']
b[:] = [entry for entry in b if entry.strip() == entry]
print(b) # -> ['a'] # CORRECT
See How to remove items from a list while iterating?.
It's considered poor form. Use a list comprehension instead, with slice assignment if you need to retain existing references to the list.
a = [1, 3, 5]
b = a
a[:] = [x + 2 for x in a]
print(b)