This question already has answers here:
Even numbers a list?
(9 answers)
Closed 5 years ago.
If I have a list of random ints and then check each individual value of it being even or odd, how would I do that?
for i in list:
if i % 2 == 0:
print('even!')
else:
print('odd!')
What I'm doing here is using a for loop to go through the list. For each element in the list, i is assigned to be the current element, then I check if i is divisible by 2 (i.e. if i is even). If it is, I print that the element is even, and if not, then I print that the element is odd.
Related
This question already has answers here:
Selecting a random list element of length n in Python
(4 answers)
How do you pick "x" number of unique numbers from a list in Python?
(7 answers)
Closed 5 years ago.
Let's say i have a list. I want to iterate over that list and add 2 random strings from it to an empty list. However, because this is a random choice, there is a possibility that the same string will be picked twice (As it is crucial not to pop or delete from that list the selected item). something like this:
import random
emptylist = []
somelist = ["a","b","c","d","e",]
for item in somelist:
emptylist.append(random.choice(somelist))
emptylist.append(random.choice(somelist))
How do i make sure that it won't pick, for example, "a" twice?
I know it is possible in many ways but im looking for the most efficient one.
This question already has answers here:
Find the index of the second occurrence of a string inside a list
(3 answers)
Find the index of the n'th item in a list
(11 answers)
Closed 7 years ago.
If I'm working with a list containing duplicates and I want to know the index of a given occurrence of an element but I don't know how many occurrences of that element are in the list, how do I avoid calling the wrong occurrence?
Thanks
I don't know that a single builtin does this thing alone, but you could fairly easily write it, for instance:
def index_second_occurence(alist, athing):
if alist.count(athing) > 1:
first = alist.index(athing)
second = alist[first + 1::].index(athing)
return second + first + 1
else:
return - 1
This question already has answers here:
List slicing with dynamic index on [:index]
(4 answers)
Closed 7 years ago.
I have a list of items in Python and I need to get "all but the last N" items. It needs to work when N is zero (in which case I want the whole list) and when N is greater than or equal to the length of the list (in which case I want an empty list).
This works in most cases:
mylist=[0,1,2,3,4,5,6,7,8,9]
print( mylist[:-n] )
But it fails in the case where N is zero. mylist[:0] returns an empty list: []. Is there a Python slicing notation that will do what I want, or a simple function?
You can pass None to the slice
print(mylist[:-n or None])
This question already has answers here:
How to find the last occurrence of an item in a Python list
(15 answers)
Closed 8 years ago.
For example, I have a list
[0,2,2,3,2,1]
I want to find the index of the last '2' that appears in this list.
Is there an easy way to do this?
You can try the following approach. First reverse the list, get the index using L.index().
Since you reversed the list, you are getting an index that corresponds to the reversed, so to "convert" it to the respective index in the original list, you will have to substract 1 and the index from the length of the list.
n = ...
print len(L) - L[::-1].index(n) - 1
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
good primer for python slice notation
I have this Python code; items is a list of tuples:
# Print the first 20
for item in items[:20]:
print item[0], item[1]
It prints the first twenty elements of the list. If the list has fewer than twenty elements, it still works, but I don't understand why. How do I interpret it?
If the passed value exceeds the number of list elements, slice is limited by the length of the list.
l = range(1,2)
l[:10] == l