See if any value exists in list by looping [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I do not know what I am missing but I've tried 3 different ways to achieve basically the same thing. Looking at the code below, why does only 1 out of the 4 ways work. I want to see if a value (located in a list) exists inside another list. I checked this SO question but still not understanding why the code is failing to print True1 , True2, and True4.
l1 = ["bravo", "alhpa", "charlie"]
l2 = ["alpha"]
if l1[1] in l2:
print "True1" # does not work
if l1[1] == l2[0]:
print "True2" # does not work
if "alpha" in l2:
print "True3" # works
for outer in l1:
for inner in l2:
if outer == inner:
print "True4" # does not work

You have a typo: "alhpa" vs. "alpha"

Related

Can Someone help me in removing two single quotes for a string inside list in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
list1 = [''ABC'',''XYZ'',''PQR'',''MNS'']
to
list1 = ['ABC','XYZ','PQR','MNS']
#I am using this code but it is not working
for i in range(0,len(list1)):
list1[i] = list1[i].replace("''","")
I find list comprehension to be the cleanest solution:
list2 = [s.replace("'", "") for s in list1]
In the snippet above, I iterate through the list binding s to each of the characters (as we traverse the list). In each iteration, I do the replacement.

Use Sum() function with lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is the correct syntax for using Sum() function in python for lists?
Say we have a list called list_1. When I tried these two syntax I get the same result:
total_1 = Sum(list_1)
total_2 = list_1.Sum()
What is the best syntax for that?
To sum a list of integers or floats, the correct method is:
list_1 = [1, 2, 3]
total = sum(list_1)
print(total)
which results in the output:
6

list comprehension using if [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to write a one-line list comprehension that takes a list of tuples and adds them to a new list if the second element of the tuple is not equal to 0
my_list = [x in my_list if x[1] != 0]
this is the code I wrote, says I have invalid syntax
The correct way
my_list = [x for x in my_list if x[1] != 0]

Python in proccess of geting to pig game [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Issue occurred right after I added the and x= "J1234": I don't know what's going on.
original = ""
if len(original) > 0 and x = "J123":
raw_input("Enter a word:")
original = raw_input('word')
print "original"
else:
print "empty"
x.isalpha()
So, you're getting a SyntaxError. To fix this, change:
x = "J123":
to...
x == "J123":
In Python the former is for setting values and the latter is for checking them. Therefore when you use this the wrong way round the syntax is incorrect.

INVALID SYNTAX ERROR for 'else' statement in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to write a quicksort program in python, however I'm getting an invalid syntax error at else statement in the second last line below:
import random
n=int(raw_input("Enter the size of the list: ")) # size of the list
intlist = [0]*n
for num in range(n):
intlist[num]=random.randint(0,10*n)
pivot=random.choice(intlist)
list_1=[] # list of elements smaller than pivot
list_2=[] # list of elements greater than pivot
for num in range(n):
if num<=pivot:
list_1.append(num)
else
list_2.append(num)
This is not a complete program as I am still writing.
add a colon after the else so that it looks like else:. and pick up a good tutorial ;)
Looks like you need a ':' after "else".

Categories

Resources