Basic function understanding (original) (raw)
Hello everyone,
I’m new here ant with python in general and I would be happy to receive support and guidance from you all.
I wrote a very easy function that should return a list of objects found in both the provided lists.
However, the code has no error but it does not return the expected outcome: the new list.
I cannot figure out the problem.
def com_country(a, b):
country = []
for x in a:
if x in b:
country.append(x)
return country
Andrea = ["Spain", "UK", "USA", "Israel", "Italy"]
Thea = ["South Africa", "USA", "Morocco", "Italy", "Israel"]
com_country(Andrea, Thea)
Thank you in advance
JamesParrott (James Parrott) May 1, 2025, 2:47pm 2
Are you just not storing the output from the function call in a new list, nor printing it?
com.py
def com_country(a, b):
country = []
for x in a:
if x in b:
country.append(x)
return country
Andrea = ["Spain", "UK", "USA", "Israel", "Italy"]
Thea = ["South Africa", "USA", "Morocco", "Italy", "Israel"]
print(com_country(Andrea, Thea))
>python com.py
['USA', 'Israel', 'Italy']
merlinz01 (Merlin) May 1, 2025, 4:49pm 3
A more compact way to do this would be to use set
objects and find their intersection:
>>> Andrea = ["Spain", "UK", "USA", "Israel", "Italy"]
>>> Thea = ["South Africa", "USA", "Morocco", "Italy", "Israel"]
>>> # Using the & operator (not the "and" keyword)
>>> print(set(Andrea) & set(Thea))
{'Italy', 'USA', 'Israel'}
>>> # Same thing but using the intersection method
>>> print(set(Andrea).intersection(set(Thea)))
{'Italy', 'USA', 'Israel'}
L.Andre (Andrea) May 1, 2025, 5:17pm 4
HI Guys, thank you for your answers!
I see, and indeed, it is working now. Thank you @JamesParrott
And thanks to @merlinz01 too. You’ve just tought me something I wasn’t aware about and that’s great.
L.Andre (Andrea) May 2, 2025, 4:57pm 5
I’m not sure if I should open another thread, or continue with this since it is related to the topic.
I have this classic password_creator function and there is something not clear about the last step.
import random
# I define the function name and an EMPTY object that will represent my password at the end: pw = str()
def password(length):
pw = str()
characters = "abcdefghijklmnopqrstuvwxyz0123456789!@#"
for i in range(length):
# this will call for looping into the "characters" object as many time as established by "length"
# range has 3 values; (start, stop, step) if not defined, start and step are = 1
pw = random.choice(characters)
print (pw)
so, if I finish this way and I call the function by typing for example
password(7)
I will only get a string of 2 characters however, if I replace the last part with this one
import random
# I define the function name and an EMPTY object that will represent my password at the end: pw = str()
def password(length):
pw = str()
characters = "abcdefghijklmnopqrstuvwxyz0123456789!@#"
for i in range(length):
# this will call for looping into the "characters" object as many time as established by "length"
# range has 3 values; (start, stop, step) if not defined, start and step are = 1
pw = pw + random.choice(characters)
print (pw)
password(7)
then I actually get a seven character password.
My question is: why I get only 2 character in the first example?
JamesParrott (James Parrott) May 2, 2025, 6:34pm 6
In the first example the previous characters in the password are overwritten. In the second, the new one is appended/concatenated to the old ones.
L.Andre (Andrea) May 3, 2025, 9:44am 7
You mean I’m replacing the letters “p” and “w” with the two randomly chosen ones? So why, in the second example, it doesn’t add the seven characters to the two of “pw”?
I’m wondering what is the rationale behind it. Sorry if I bother but I’m trying to understand the “grammar/rules” of this language, for a better use of it.
Thank you
JamesParrott (James Parrott) May 3, 2025, 10:27am 8
You’re confusing variable names, with the actual value of the variable (which in this case is a string, but in Python coulld be anything).