I receive a list in a list on this example:
y.append([x for x in range(0,6)])
The result:
[[0,1,2,3,4,5]]
How can I remove one []?
y.extend([x for x in range(0,6)])
or if you want to assign it to another value
a = [x for x in range(0,6)]
Just do this:
y.append([x for x in range(0,6)])
new_list = y[0]
new list will be the first list bit without one set of braces
Related
So I have a nested list and would like to compare and remove a list inside a nested list based on the condition match.
Here is my code :
def secondValue(val):
return val[1]
if __name__ == '__main__':
nestedList=[]
for _ in range(int(input())):
name = input()
score = float(input())
nestedList.append([name,score]) # Made a nested list from the input
lowestMarks=min(nestedList,key=secondValue) [1] #Extracting the minimum score
newList=[x for x in nestedList[1] if x!=lowestMarks] # PROBLEM HERE
The last line of my code is where I want to remove the list inside my nested list based on condition match. Sure, I can do this with a nested for loop but if there is a way to do this using list comprehension I'd consider that approach.
Basically I'd appreciate an answer that tells how to remove a list from a nested list based on a condition. In my case the list looks like :
[[test,23],[test2,44],......,[testn,23]]
Problems:
for x in nestedList[1] just iterates over second sublist of the nested list.
x is a sublist and it can never be equal to lowestMarks.
Use a list-comprehension as:
newList = [[x, y] for x, y in nestedList if y != lowestMarks]
Mistake was in the below line and is now fixed.
newList=[x for x in nestedList if x[1] != lowestMarks] # PROBLEM HERE
nestedList[1] fetches the second sub-list. You want to iterate over the entire list.
I have a Python list like:
['user#gmail.com', 'someone#hotmail.com'...]
And I want to extract only the strings after # into another list directly, such as:
mylist = ['gmail.com', 'hotmail.com'...]
Is it possible? split() doesn't seem to be working with lists.
This is my try:
for x in range(len(mylist)):
mylist[x].split("#",1)[1]
But it didn't give me a list of the output.
You're close, try these small tweaks:
Lists are iterables, which means its easier to use for-loops than you think:
for x in mylist:
#do something
Now, the thing you want to do is 1) split x at '#' and 2) add the result to another list.
#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split('#')
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append(split_results[1])
Once you understand that well, you can get fancy and use list comprehension:
newlist = [x.split('#')[1] for x in mylist]
That's my solution with nested for loops:
myl = ['user#gmail.com', 'someone#hotmail.com'...]
results = []
for element in myl:
for x in element:
if x == '#':
x = element.index('#')
results.append(element[x+1:])
I have a list of tuples and some of the tuples only have one item in it. How do I remove tuples with only one item from the list? I want to keep tuples with two items in it.
The tuples I have contain a string, and then an integer after it
list = ((['text'],1),(['text'],2),((3,))
I may suggest:
filtered_list = [tup for tup in list if len(tup) == 2]
You can also check if tuple length is higher than one or anything else...
What about:
new_list = [x for x in old_list if len(x) > 1]
You want to create a list with squared brackets instead of parentheses, otherwise you'd create a tuple.
Also please don't call your variables like built-in names as EdChum suggested.
The solution here is to filter your list:
l=[(1,2),(3,),(4,5)]
filter(lambda x: len(x)!=1, l)
You could use something like that to rebuild a new tuple with tuple that have a length greater than 1.
new_list = tuple(item for item in your_list if len(item) > 1)
How to check if an element in the list exists in another list?And if it does append it to another list.How Can i make it to get all the values in a list?
common=[]
def findCommon(interActor,interActor1):
for a in interActor:
if a in interActor1:
common.append(a)
return common
interActor=['Rishi Kapoor','kalkidan','Aishwarya']
interActor1=['Aishwarya','Suman Ranganathan','Rishi Kapoor']
You can do it using list comprehensions:
common = [x for x in iter_actor1 if x in iter_actor2]
or using sets:
common = set(iter_actor1).intersection(iter_actor2)
interActor=['Rishi Kapoor','kalkidan','Aishwarya']
interActor1=['Aishwarya','Suman Ranganathan','Rishi Kapoor']
anotherlist = []
for x in interActor:
if x in interActor1:
anotherlist.append(x)
I have a list inside a list and I am trying to remove any values in side the nested list that are equal to -1. I am getting a "ValueError: list.remove(x): x not in list" error when I try to run my code, any idea what I am doing wrong?
for x in list:
for i in x:
if i == -1:
list.remove(x)
You shouldn't mutate a list while iterating over it. You also shouldn't name a variable list, since that name is used by a built-in function. You can achieve what you want via a simple list comprehension:
my_list = [[x for x in v if x != -1] for v in my_list]