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
Related
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
This question already has answers here:
Different ways of clearing lists
(8 answers)
Closed 4 years ago.
list1 = [2,5,61,7,10]
list1.remove(list1[0:len(list1)-1])
print(list1)
I want to remove all elements from that list but it shows me syntax error.
Any idea how can I remove all elements and print the final result like []
To remove all list items just use the in-built .clear() function:
>>> list1 = [2,5,61,7,10]
>>> list1.clear()
>>> print(list1)
[]
>>>
If you want to remove all elements from a list, you can use the slice assignment:
list1[:] = []
But with list.remove(), you can only do it one-by-one:
for item in list(list1):
list1.remove(item)
Note I created a copy of list1 with the for loop because it's dangerous to modify what you're iterating over, while it's safe to modify the list while iterating over a copy of it.
To remove some of the items:
for item in list1[0:3]:
list1.remove(item)
Or just better yet, use slice assignment:
list1[0:3] = []
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:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 7 years ago.
I have a list
test_list = [1,2,3,4,5]
I want to iterate over the elements of this list and delete them after using. But when I try to do this
for element in test_list:
print element
test_list.remove(element)
Alternate elements are printed and removed from test_list
1
3
5
print test_list
[2, 4]
Please explain why this happens!
Read the answers to strange result when removing item from a list to understand why this is happening.
If you really need to modify your list while iterating do this:
>>> items = ['x', 'y', 'z']
>>> while items:
... item = items.pop()
... print item
...
z
y
x
>>> items
[]
Note that this will iterate in reverse order.
in python this concept is called an iterator
my_iter = iter(my_list)
each time you consume or look at an element it becomes gone ...