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(",")])
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:
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)
This question already has answers here:
What is the id( ) function used for?
(13 answers)
Closed 2 years ago.
I knew when python's list append element,the element is appended to tail. I tried to output the element of the list, why the element's address is out of order? Please help me out,thanks!
list = []
list.append(2)
list.append(10)
list.append(3)
print('--append--')
for i in list:
print('i:{}, id:{}'.format(i,id(i)))
the output is:
--append--
i:2, id:140711739437936
i:10, id:140711739438192
i:3, id:140711739437968
The id() function returns a unique id for the specified object.
You need to use the index of the list
list = []
list.append(2)
list.append(10)
list.append(3)
print('--append--')
for i in list:
print('i:{}, id:{}'.format(i,list.index(i))) # replace id with list.index
id() returns identity (unique integer) of an object...
a=3
print(id(3)) #9752224
You can use this
list = []
list.append(2)
list.append(10)
list.append(3)
print('--append--')
for i in enumerate(list): #enumerate return an enumerate object.if list it [(0,2),(1,10),(2,3)]
print('i:{}, id:{}'.format(i[1],i[0]))# for getting index number i[0]
The id function is rarely used in actual programming, and usually, you use the list index to deal with lists. Your example will look something like:
mylist = []
mylist.append(2)
mylist.append(10)
mylist.append(3)
print(mylist)
Output:
[2,10, 3]
Sample code:
for x in range(len(mylist)):
print(x, mylist[x])
Output:
0, 2
1, 10
2, 3
You may check one of the good python tutorials on the web such as the one on the python web page: https://docs.python.org/3/tutorial/
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:
Appending the same string to a list of strings in Python
(12 answers)
Closed 3 years ago.
Let's say I have a list:
list = ["word", "word2", "word3"]
and I want to change this list to:
list = ["word:", "word2:", "word3:"]
is there a quick way to do this?
List comprehensions to the rescue!
list = [item + ':' for item in list]
In a list of
['word1', 'word2', 'word3']
This will result in
['word1:', 'word2:', 'word3:']
You can read more about them here.
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
You can use a list comprehension as others have suggesed. You can also use this code:
newlist = map(lambda x: x+':', list)
simple question but though a noob friendly answer :)
list = ["word", "word2", "word3"]
n = len(list)
for i in range(n):
list[i] = list[i] + ':'
print list
strings can be concatenated using '+' in python and and using 'len' function you can find the length of list. iterate i using for loop till n and concatenate ':' to the list eliments.
feel free to post but thing of google first :P