Python. Removing list from another list [duplicate] - python

This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 1 year ago.
I have 2 lists
for i in range(1, 26):
U.append(i)
A = [8,9,10,11,12,16,17,22,25]
How can I extract A values from U list?

Use a list comprehension:
[item for item in U if item not in A]

Related

How do you add integers of a list to another list? (Python) [duplicate]

This question already has answers here:
Element-wise addition of 2 lists?
(17 answers)
Closed 7 months ago.
So I have a list of integers list1=[1,2,3,4], and I want to know how I can add integers from another list list2=[1,2,3,4], so it will add the numbers from list2 to list1: list1=[2,4,6,8]. I have tried many ways but I cannot figure out how to do this.
list_res = [x + y for (x, y) in zip(list1, list2)]

remove special characters to get string in python list [duplicate]

This question already has answers here:
Get the first element of each tuple in a list in Python [duplicate]
(5 answers)
Closed 2 years ago.
I have the following code which gives me list as shown below
listP= methodcall()
print(listP)
Output [('abcd',), ('efgh',)
How do I remove bracket,comma and single quotes?
I need below output:-
listP=['abcd','efgh']
This will do the work:
listp = [('abcd',), ('efgh',)]
listp = [l[0] for l in listp]
print(listp)
Take only first element from the tuples in the original list using list comprehension.
newlist = []
list1 = [('abcd',), ('efgh',)]
for elements in list1:
newlist.append(elements[0])
print(newlist)

list comprehension even list elements instead of loop and how to refer only to the index [duplicate]

This question already has answers here:
Shortest way to slice even/odd lines from a python array?
(4 answers)
Closed 3 years ago.
I want to use list comp' instead of a loop.
Let's sat I have a list of lists and I want only the even index elements.
Eg: [[a,a], [b,b], [c,c],[g,g]] to..... [[a,a], [c,c]]
This doesn't work
a = [i for i in the_list if i % 2 == 0)]
Here is a possible solution that keeps only lists with even indexes:
result = [lst for i, lst in enumerate(the_list) if i % 2 == 0]

how to iterate through two lists in python and match up corresponding items [duplicate]

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 7 years ago.
I have two lists:
nums = [41.25, 38.75, 43.25, 37.25, 37.5, 43.75]
sats = [G01, G03, G04, G11, G28, G32]
note: the first item in nums corresponds to first item in sats, the second item in nums corresponds to the second item in sats etc..
I want to loop through nums and where the value is less than 39.00, I want to get its corresponding item from sats?
can anyone help?
for num,sat in zip(nums, sats):
if num<39:
# do stuff

pythonic way to flatten nested lists? [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Apply function to each element of a list
(4 answers)
Closed 6 months ago.
I have a list-tuple-list tree and I'd like to flatten it for a query, so that I can print all the items of the the first lists of each tuple.
I can do it via a for loop:
bigNest = [([item1,item2],[]),([item3],[item4])]
mergedlist = []
for listItem in bigNest:
mergedlist += listItem[0]
print mergedList
Is there a simpler/quicker way which will also work with larger tuples?
edit: sorry the last post was wrong, you could do a list comprehension:
mergedlist = []
foovar = [mergedlist.extend(i[0]) for i in bigNest]

Categories