This question already has answers here:
Add a character to each item in a list [duplicate]
(3 answers)
Closed 9 years ago.
I have a string which I want to concatenate with every object within a list. Here is an example:
a = ['1','2']
b = 'a'
and I want:
c = ['a1','a2']
It seems that strings can't be concatenated to list objects directly so I assume that I should convert my list to the string and then add it. Is it correct or any suggestions?
Try Python list comprehensions.
>>> a = ['1','2']
>>> b = 'a'
>>> [b+i for i in a]
['a1', 'a2']
Related
This question already has answers here:
How to convert a string of space- and comma- separated numbers into a list of int? [duplicate]
(6 answers)
Closed 18 days ago.
I have this:
"1,2,3,4,5,6,7,8,9,0"
And need this:
[1,2,3,4,5,6,7,8,9,0]
Everything I search for has the example where the string is a list of strings like this:
"'1','2','3'..."
those solutions do not work for the conversion I need.
What do I need to do? I need the easiest solution to understand for a beginner.
You could use a mapping to convert each element to an integer, and convert the map to a list:
s = "1,2,3,4,5,6,7,8,9,0"
l = list(map(int, s.split(',')))
print(l)
You can use str.split to get a list, then use a list comprehension to convert each element to int.
s = "1,2,3,4,5,6,7,8,9,0"
l = [int(x) for x in s.split(",")]
print(l)
You can just call int("5") and you get 5 like a int.
In your case you can try list comprehension expression
a = "1,2,3,4,5,6,7,8,9,0"
b = [int(i) for i in a.split(',')]
print(b)
>> 1,2,3,4,5,6,7,8,9,0
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 1 year ago.
so how can I get a word like dog and make it into "d","o","g" with a function in python?
Thanks.
Strings are iterable: each element is a single-character string.
for c in "dog":
print(c)
d
o
g
list takes an arbitrary iterable as an argument, and creates a list with one element per value from that iterable.
>>> list("dog")
['d', 'o', 'g']
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
x = ['[1867, 1868]', '[6612663]']
Expected Output:
x = [[1867, 1868], [6612663]]
I tried ,
x = [item.replace("'", "") for item in x]
It didn't work.
Could someone help?
You can use ast.literal_eval with a list comprehension:
import ast
x = ['[1867, 1868]', '[6612663]']
new = [ast.literal_eval(ls) for ls in x]
print(new)
Output:
[[1867, 1868], [6612663]]
This question already has answers here:
Finding the index of an item in a list
(43 answers)
Closed 8 years ago.
What mechanism is used that allows the built-in function list.remove but not simply list.find?
If I have a list l = [a,b,c...] and want to remove an element, I don't need to know its index, I simply input l.remove(element). Why is it then that I can't use a similar command to find an element's index or to simply check if it's in the list?
Interesting that it's not list.find, but list.index:
>>> l = ['a', 'b', 'c']
>>> l.index('c')
2
To test for membership:
>>> 'b' in l
True
Which is equivalent to (and should be used instead of):
>>> l.__contains__('b')
True
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Python split string into multiple string [duplicate]
(3 answers)
python chain a list from a tsv file
(3 answers)
Closed 8 years ago.
Can any one tell me how to split a list, if its possible. Want to split it word by word.
my list contains links like:
['14th_century;15th_century;16th_century;Pacific_Ocean;Atlantic_Ocean;Accra;Africa;Atlantic_slave_trade;African_slave_trade']
Now, i want to use the split method, to split up 14th_century and 15th_century, so it is 2 words, and so on with all links.
So for every sign " ; " it should just split it.
right now i made a for loop.
for line in loops:
UPDATE:
have done it so far as this.
links = []
for line in newPath:
links.append(line[3:4])
old_list = []
new_list = []
old_list = links
new_list = old_list[0].split(';')
print new_list
You can simply do:
my_list = old_list[0].split(';')
Examples
>>> old_list = ['14th_century;15th_century;16th_century;Pacific_Ocean;Atlantic_Ocean;Accra;Africa;Atlantic_slave_trade;African_slave_trade']
>>> my_list = old_list[0].split(';')
['14th_century', '15th_century', '16th_century', 'Pacific_Ocean', 'Atlantic_Ocean', 'Accra', 'Africa', 'Atlantic_slave_trade', 'African_slave_trade']
You can simply do:
paths = ['abc;def;ghi', 'jkl;mno;pqr']
paths = [path.split(';') for path in paths]
>>> paths
[['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']]