A simpler way to do something for the 1st item of A in C that is also in B without iterating twice - python-3.x

I have constructed the following script in Python 3 that does what it needs to, but, it iterates through my items twice. Is there are way to the same outcome with a single iteration?
if any(A in B for A in C):
for A in C:
if A in B:
# Do something with A.
# Order of iteration is important.
break
else:
# Do something else

for loops can also have else clauses, which they enter if you don't break out of them. So your loop can be written
for A in C:
if A in B:
# Do something
break
else:
# Do something else

The most efficient way would probably to get A in a single iteration at the C-level (using filter and next) and then use it straight away.
A = next(filter(B.__contains__, C), None)
if A is not None:
# Do something with A
else:
# Do something else

Related

Python Compare Two lists with a loop and if a match is found run a few codes, and return back to the loop to test if there is another match

I am building a program in python. I have two lists and I want to loop through each of them to see if the elements are equal if they are I want to remove the one element from the list and I want to run a few codes. When this is done I want to return back to the loop to compare the lists again. until one list is empty and the program will continue.
I tried this but just can not get my mind to unblock it! Thank you.
x = [1,2,3,4,5,6,7]
y = [28,1,2,3,4,5,6]
for i in x:
for a in y:
while i == a:
x.remove(i)
How do I tell python to return to the first loop??
Use flag approch
x = [1,2,3,4,5,6,7]
y = [28,1,2,3,4,5,6]
flag=False
for i in x:
flag=False
for a in y:
while i == a:
x.remove(i)
flag=True
if(flag==True):
break

Reverse a singly linked list

Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Here's what I saw online:
class Solution:
def reverseList(self, head):
def reverse(prev, cur):
if cur:
reverse(cur, cur.next)
cur.next = prev
else:
nonlocal head
head = prev
reverse(None, head)
return head
But I didn't get the process how it works after the if cur.
For example the test case is [1,2,3,4,5].
input None, 1 into the reverse function
cur exist, run reverse function with 1, 2
...
cur exist, run reverse function with 4, 5
cur does not exist, (then what???)
BTW why does listNode even exist? It is not as easy as others like list array or dictionary. When should i use it?
There's no efficient way to do this. The only way without interacting with the list beforehand, is an algorithm of a complexity of O(n * n), where you repeatedly go through the linked list, find last element and remove it, while adding it to a new linked list, until the first list is completely empty. If you wanted to preserve that list, you'd have to recreate that list during the reversing or simply copy it beforehand, not fun at all. This is what example you posted does.
def reverse(prev, cur):
if cur: # this should actually be if cur is not None:
reverse(cur, cur.next) # call same method on next node
cur.next = prev # when we are back from recursion hell, set previous one as next one, because we are reversing
else: # we finally reached the end
nonlocal head # excuse me, this is in a class but you wrap in multiple functions and use nonlocal??? Where did you even dig out this thing from?
head = prev # head now becomes the previous node, because this node is None, or, end of the list
If you just want to learn about them, then this algorithm is perfectly fine, in any other case, you should at least upgrade to doubly linked list, you'll use insignificant amount of memory to speed up reversal a lot.
If you create your own doubly linked list, reversing could be as fast as swapping end and start "pointers", if you write it in a way where every node has two references, left and right in a list, such as neighbours = [left, right], your linked list could store a value called advance direction, which would be either 0 or 1, meaning that you could reverse entire list from start to end while going to left to right to end to start while going right to left by merely swapping start with end and changing "advance value" from 1 to 0. The advance value would be wrapped in a method like next(), which would look something like this:
def next(self):
if self.current is None:
return None # could be return self.current or even just "return", this is Python
self.current = self.current.neighbours[self.advance_value]
return self.current
When advance_value is 0, it goes left, when advance_value is 1, it goes right through the list, all elements stay in their place, but to whoever is accessing it, it looks like it's reversed.

Whenever I have two conflicting greater than how do I set a limit there

Hi I'm kind of new to programming. I used Game Maker before this so I need help.
Whenever I have two conflicting greater than how do I set a limit there. Example:
if (weight>=20): (print(example))
if (weight>=30): (print(example)) So if I were to type in 35 it would print both things. So how do I make it print only one thing. Thank you for your time.
P.S I'm using python 3.6.
The answer is in the elif statement
if x > a:
# x is greater than a
#do something
pass
elif x > b:
# x is not greater than a, but greater than b
# do something
pass
else:
# do something else
pass

Python: find the first even number in a list using a while loop

I'm trying to write a function that returns the first even number in a list, using a while loop
def first_even(list):
while i%2 !=0: #I'm not sure if this is a good start for the while loop, but I think I'm supposed to use something%2...
#no idea what to do here
A good and very short coding style will be:
for i in list:
if i%2 == 0:
return i

changing the value of iterator of for loop at certain condition in python

Hello friend while learning python it came into my mind that is there any way by which we can directly jump to a particular value of iterator without iterating fro example
a=range(1.10) or (1,2,3,4,5,6,7,8,9)
for i in a
print ("value of i:",i)
if (certain condition)
#this condition will make iterator to directly jump on certain value of
#loop here say if currently i=2 and after this it will directly jump the
#the iteration value of i=8 bypassing the iterations from 3 to 7 and
#saving the cycles of CPU)
There is a solution, however it involves complicating your code somewhat.
It does not require an if function however it does require both while and try loops.
If you wish to change the numbers skipped then you simply change the for _ in range() statement.
This is the code:
a = [1,2,3,4,5,6,7,8,9,10]
at = iter(a)
while True:
try:
a_next = next(at)
print(a_next)
if a_next == 3:
for _ in range(4, 8):
a_next = next(at)
a_next = str(a_next)
print(a_next)
except StopIteration:
break
The iterator interface is based on the next method. Multiple next calls are necessary to advance in the iteration for more that one element. There is no shortcut.
If you iterate over sequences only, you may abandon the interator and write an old-fashioned C-like code that allows you to move the index:
a = [1,2,3,4,5,6,7,8,9,10]
a_len = len(a)
i = 0
while i < a_len:
print(a[i])
if i == 2:
i = 8
continue
i += 1

Resources