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)
Related
This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed last year.
Suppose I have two list of lists as follows:
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
How would I get the above as:
List3 = [{1:1.2,2:2.4},{3:2.4,4:5.0},{3:4.5,8:6.0}]
Try this:
List3 = [dict(zip(*lsts)) for lsts in zip(List1, List2)]
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
List3=[]
for i,e in enumerate(List1):
List3.append(dict(zip(List1[i],List2[i])))
print(List3)
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]
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I wrote the following python code in jupiter-notebook:
x=['b','x','a']
y=['b','x','a']
for i in x:
if i in y:
x.remove(i)
print (x)
I expected an empty list, but the output was ['x'].
What is wrong in the code?
You are getting this behavior because you are removing items in the list x while iterating on it.
To have the result you expect (an empty list), you should use a list comprehension :
x = [i for i in x if i not in y]
print(x) # prints []
Check this StackOverflow question on the same subject : How to remove items from a list while iterating?
The bug is happening because you are removing an element of the list x while you are iterating.
You don't have to iterate. Try this:
set(x)-set(y)
Here you can use the filter-function, as such
x = list(filter(lambda element: element not in y, x))
See http://book.pythontips.com/en/latest/map_filter.html#filter
This question already has answers here:
How do I make a flat list out of a list of lists?
(35 answers)
Closed 4 years ago.
I'm looking for a way to sum up my output of list. I want to add each output of this for loop into one "big" list as a single list element.
for line in f.split('\n'):
words = line.split()
How do I achieve this:
L1 = [1,2,3]
L2 = [4,5,6]
L3 = [7,8,9]
SumList = [[1,2,3],[4,5,6],[7,8,9]]
You can append str.split list to another list
Ex:
res = []
for line in f.split('\n'):
res.append(line.split())
Or use list comprehension
Ex:
print([line.split() for line in f.split('\n')])
This question already has answers here:
How to convert a string list into an integer in python [duplicate]
(6 answers)
Closed 7 years ago.
I have a list that looks like this:
mylist = ['1,2,3']
It is a list of one string. I want to convert this to a list of integers like:
mylist = [1,2,3]
I tried [int(x) for x in mylist.split(',')] but it doesn't work.
Can someone please help? Thanks in advance.
Using list comprehension. split is a string method
[int(j) for i in mylist for j in i.split(',')]
The reason your code doesn't work is that your list only contains one item - "1,2,3". Split the first (and only) item in the list on comma, then map int to the items you get:
mylist = ['1,2,3']
print map(int, mylist[0].split(","))
Prints
[1, 2, 3]
If you have more than one item in your list, you can do
print map(int, [sub for item in mylist for sub in item.split(",")])