This question already has answers here:
How to check if a tuple contains an element in Python?
(4 answers)
Closed 18 days ago.
how do i find a certain element in a tuple and print out the index of the element.
a = (1,2,3,4)
b = int(input('enter the element you want to find'))
if b in a :
print('found')
here is the simple program. now how do print the index of the element whic needs to be searched
Use .index:
if b in a:
print(a.index(b))
You can use enumerate as well:
enumerate
a = (1,2,3,4)
b = int(input('enter the element you want to find'))
for x,y in enumerate(a):
if b == y:
print(x)
Edit:
to find the multiple indices of an element:
Suppose we have b at 3 positions, now if you want to find all the three indices then:
a = (1,2,3,4,5,6,2,8,2)
b = 2
for i, x in enumerate(a):
if x == b:
print(i)
1
6
8
with list comprehension :
[i for i, x in enumerate(a) if x == b]
#[1, 6, 8]
Use:
print(a.index(b))
Use this link for working with tuples
Related
This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 4 years ago.
I have list like this:
a = [1,2,3,4,5,6,7]
b = [10,11,13,2,14,7]
I want output like this:
b = [10,11,13,14]
if an element of a is in b then it has been discarded.
please, anyone can tell me how to do this?
Using list comprehension:
b = [x for x in b if x not in a]
Works like this:
a = [1,2,3,4,5,6,7]
b = [10,11,13,2,14,7]
b = [x for x in b if x not in a]
print b
>> [10, 11, 13, 14]
Re: #DeepSpace's suggestion, looking for elements of a set in a list will go significantly faster than looking for elements of a list in another list, so declare a as a set()
a = set([1,2,3,4,5,6,7])
b = [10,11,13,2,14,7]
b = [x for x in b if x not in a]
This question already has answers here:
Finding the index of an item in a list
(43 answers)
Closed 4 years ago.
I want to find the index of values that contain a keyword in an array.
For example:
A = ['a1','b1','a324']
keyword = 'a'
I want to get [0,2], which is the index of a1, a324
I tried this list(filter(lambda x:'a' in x, A))
But get ['a1','a324'] rather than the index.
Use enumerate with a list-comprehension:
A = ['a1','b1','a324']
keyword = 'a'
print([i for i, x in enumerate(A) if keyword in x])
# [0, 2]
Simply write:
A = ['a1','b1','a324']
keyword = 'a'
indices = [i for i in range(len(A)) if keyword in A[i]]
print(indices)
This question already has answers here:
How to extract the n-th elements from a list of tuples
(8 answers)
Closed 4 years ago.
Am trying to print elements of index 1 like 4994 2993 100 20.5 from my tuple but it print ('dan', '2993').
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
print(b[1])
Have searched on this site and none of the answer provided gives me clue to solve this.
You can use unpacking in a list comprehension:
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
new_b = [i for _, i in b]
Output:
['4994', '2993', 100, 20.5]
Another list comprehension way:
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
new_b = [i[1] for i in b]
# ['4994', '2993', 100, 20.5]
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
for item in b:
print(item[1])
In this case you are trying to access the second element of a two-items tuple. Using slicing would be:
>>> b[0][1]
and you will get as output>
>>> '4994'
If you put this tuple into a for loop, the first element (item in my code) would be: ("ben","4994") and when you print(item[1]) you are having access to the second level of slicing, that is: '4994' in the first running of the loop and so on.
This question already has answers here:
Accessing the index in 'for' loops
(26 answers)
Closed 8 years ago.
I have a list U of lists such that U=[a[1], a[2], a[3]] and where a = [[0,1,2], [0,0,0],[1,1,1]].
I am trying to change U in a way that it will only contain lists without 2. I know that if I want U = [a[2], a[3]] I have to do it like this:
for j in range (0,2):
U= [f for f in U if f[j]!=2]
But now I want it to return only the index of the lists, I mean at the end U should be [2,3].
Is it possible to do it ?
Thanks for the help !
Use enumerate:
>>> a = [[0,1,2],[0,0,0],[1,1,1]]
>>> [i for i, x in enumerate(a, 1) if 2 not in x]
[2, 3]
Use enumerate:
[i for i, j in enumerate(a) if 2 not in j]
note that list indices in Python start from 0, not from 1
you can use index
[U.index(l) for l in U if not 2 in l]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 9 years ago.
Following on my previous question How to group list items into tuple?
If I have a list of tuples, for example
a = [(1,3),(5,4)]
How can I unpack the tuples and reformat it into one single list
b = [1,3,5,4]
I think this also has to do with the iter function, but I really don't know how to do this. Please enlighten me.
b = [i for sub in a for i in sub]
That will do the trick.
In [11]: list(itertools.chain(*a))
Out[11]: [1, 3, 5, 4]
If you just need to iterate over 1, 3, 5, 4, you can get rid of the list() call.
Just iterate over the list a and unpack the tuples:
l = []
for x,y in a:
l.append(x)
l.append(y)
Another way:
a = [(1,3),(5,4)]
b = []
for i in a:
for j in i:
b.append(j)
print b
This will only handle the tuples inside the list (a) tho. You need to add if-else statements if you want to parse in loose variables too, like;
a = [(1,3),(5,4), 23, [21, 22], {'somevalue'}]
b = []
for i in a:
if type(i) == (tuple) or type(i) == (list) or type(i) == (set):
for j in i:
b.append(j)
else:
b.append(i)
print b
import itertools
b = [i for i in itertools.chain(*[(1,3),(5,4)])]