This question already has answers here:
What is the most efficient way to search nested lists in python?
(5 answers)
Search in lists of lists by given index
(13 answers)
Closed 4 years ago.
I am given:
list = [{'a','b'},{'c','d'}]
and I want to know if 'a' is in the list.
should I first unpack the list as here to new_list and the use 'a' in new_list
Or is there a shorter way (without importing modules)
use any
spam = [{'a','b'},{'c','d'}]
eggs = [{'d','b'},{'c','d'}]
print(any('a' in my_set for my_set in spam))
print(any('a' in my_set for my_set in eggs))
Output
True
False
>>>
This is one approach using any.
Ex:
l = [{'a','b'},{'c','d'}]
print( any(map(lambda x: "a" in x, l)) )
Output:
True
Hope it's solve it. I write it on a function to use "return" keyword
def a_is_there():
lists = [{'a','b'},{'c','d'}]
for list in lists:
if "a" in list:
print("True")
return True
print("False")
return False
a_is_there()
Thanks
Related
This question already has answers here:
Check if an item is in a nested list
(8 answers)
Closed 2 years ago.
I'm looking to see if item is in list of list.
List1 = [['a','b','c'],['d','e','f'],['g','h','i']]
If b is in any of the list in a list, then print True. Else, print False.
What's the pythonic way to do this?
Try this:
# check element exists in list of list or not?
result = any("b" in sublist for sublist in List1)
# printing result
print(str(result))
I'll play the fool...
>>> 'b' in sum(List1, [])
True
>>> 'B' in sum(List1, [])
False
This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 5 years ago.
I have a list of strings in which there are a lot of repeated items. I would like to make a new list in which all items are present but there is only one occurrence of each item.
input:
mylist = ["hg", "yt", "hg", "tr", "yt"]
output:
newlist = ["hg", "yt", "tr"]
I actually have tried this code but did not return what I want:
newlist = []
for i in range(len(mylist)):
if mylist[i+1] == mylist[i]:
newlist.append(mylist[i])
You can simply use a set:
newlist = set(mylist)
Or, to retrieve exactly a list, but is can be useless depending what you are doing with:
nexlist = list(set(mylist))
This question already has answers here:
Check if list contains another list in python
(2 answers)
Closed 6 years ago.
I have two lists:
my_list = [1,2,3,4,5]
my_new_list = [[1,3,7,5],[1,2,3,4,5]]
How can I check that a sublist equals my_list?
If you want to check if my_list is in the my_new_list just use in:
>>> my_list in my_new_list
True
If you want to know the index of the matching list you can use index:
>>> my_new_list.index(my_list)
1
If you think these are too efficient, too easy or too short you can do it manually as well:
>>> any(sublist == my_list for sublist in my_new_list) # equivalent to "in"
True
>>> next(idx for idx, sublist in enumerate(my_new_list) if sublist == my_list) # like "index".
1
You can index built-in function
>>> my_new_list.index(my_list)
1
Or you can use in :
>>> my_list in my_new_list
True
You can also use magic function contains
>>> my_new_list.__contains__(my_list)
True
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 years ago.
How do I convert a list of lists to a single list?
Input:
a=[['AA'], ['AE'], ['AH'], ['AO'],]
Desired output:
['AA','AE','AH','AO']
a=[['AA'], ['AE'], ['AH'], ['AO'],]
l=[]
for i in a:
l.extend(i)
print l
you could use comprehension list:
or using map and lambda functions
a=[['AA'], ['AE'], ['AH'], ['AO'],]
# open the item [0] with generators
Newa = [x[0] for x in a]
>>>['AA', 'AE', 'AH', 'AO']
see examples: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
EDIT:
for i, value in enumerate(a):
a[i] = value[0]
This question already has answers here:
Finding the index of an item in a list
(43 answers)
Closed 8 years ago.
What mechanism is used that allows the built-in function list.remove but not simply list.find?
If I have a list l = [a,b,c...] and want to remove an element, I don't need to know its index, I simply input l.remove(element). Why is it then that I can't use a similar command to find an element's index or to simply check if it's in the list?
Interesting that it's not list.find, but list.index:
>>> l = ['a', 'b', 'c']
>>> l.index('c')
2
To test for membership:
>>> 'b' in l
True
Which is equivalent to (and should be used instead of):
>>> l.__contains__('b')
True