Sorry for the very basic question, but this is actually a 2-part question:
-
Given a list, I need to replace the values of ‘?’ with ‘i’ and the ‘x’ with an integer, 10. The list does not always have the same number of elements, so I need a loop that permits me to do this.
a = ['1', '7', '?', '8', '5', 'x'] -
How do I grab the index of where the value is equal to ‘?’. It’d be nice if this show me how I could grab all the index and values in a list as well.
Write a function for it and use
map()to call it on every element:Note that this creates a new list. If the list is too big or you don’t want this for some other reason, you can use
for i in xrange(len(a)):and then updatea[i]if necessary.To get
(index, value)pairs from a list, useenumerate(a)which returns an iterator yielding such pairs.To get the first index where the list contains a given value, use
a.index('?').