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]
Related
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.
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
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"
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 have a list in python like l=('A','1,''B','2','C','3,''D','4') and i need to filter out the value of A,B,C,D so i wrote a code like follow
list(filter(lambda x:x.isalpha(),l))
then returned ['A', 'C']
it did't return value B and D so i thought it was some thing i don't know about python filter function then i wrote it in list comparison like follows
[i for i in l if i.isalpha()]
but the strange thing is it also return the ['A', 'C'] so every time what happen to the value B and D
Any one can explain me how to filter all alphabetic values out?
You're malforming the list, it should be:
l=('A','1','B','2','C','3','D','4')
Notice the commas
Hope it helps!
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 7 years ago.
Improve this question
I'm trying to sort each string inside a list. For example, if I have the list ['bca', 'fed'] then I want my function to return ['abc', 'def'].
Currently trying to do this like so:
lines = [''.join.(sorted(e)) for e in lines]
What's wrong with this syntax?
Just remove the redundant dot (.):
lines = [''.join(sorted(e)) for e in lines]