How can i seperate string into individual charecters in python [duplicate] - python

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']

Related

How to append lists of strings into one list of string followed by ";" [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I've got multiple lists (list1, list2, ...) which depends on a number of elements (they may change) in another listX -> len(listX)
How to append all of these lists of strings into one string followed by ";"?
list1 = ['a', 'b']
list2 = ['c', 'd']
...
listn = ['x', 'y']
the final string should look like: 'a;b;c;d;...;x;y'
Well, how about collecting all the individual lists into a single list and then joining the individual items together.
Approach 1:- Collecting all the list and then joining them
intermidiate_list = list()
for i in range(len(mega_list)):
intermidiate_list.extend(mega_list(i))
result = ";".join(intermidiate_list)
Here mega_list is the collection of all the lists you have. You can iterate over them by indexes and still make it work.
Approach 2:- Generating the list on the fly
result = str()
small_list = get_list()
while small_list:
result += ";".join(small_list)
small_list = get_list()
if small_list and len(small_list) > 0:
result += ";"
Now iterate the logic for all the list items you generate, in the end result will have the

Make a list of strings into a string with each value on a new line [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Printing list elements on separate lines in Python
(10 answers)
Closed 5 years ago.
So, I have a project in which I have a list of strings:
outputs = ['cow', 'chicken', 'pig']
I need to turn them into a string, with each value separated by a newline, like so:
cow
chicken
pig
I found a solution at python list to newline separated value, but it uses a list like this:
outputs = [['cow'], ['chicken'], ['pig']]
and whenever I run:
answer = "\n".join(map(lambda x: x[0], outputs))
print(answer)
It returns 'c', as is the case with all the other answers in that question.
You can join a list of strings by another string by simply using:
str = "\n".join(outputs)
Your problem is that when you did:
map(lambda x: x[0], outputs)
You created an iterable consisting of the first letter of each element in outputs:
>>> outputs = ['cow', 'chicken', 'pig']
>>> list(map(lambda x: x[0], outputs))
['c', 'c', 'p']
Your over-thinking this. In this case, you don't even need to use map. You can simply use str.join:
'\n'.join(outputs)

python store string to array not characters [duplicate]

This question already has answers here:
What is the difference between Python's list methods append and extend?
(20 answers)
Closed 5 years ago.
I'm new to python. I tried to store bunch of strings to an array and at the end print out the array, however it print out as a long list of characters. here is my code:
user_with_no_records = [""]
for user_test_docs in json_data['results']:
... do something here ...
user_with_no_records.extend(user_test_docs['userId'].replace("'", '"'))
...
pprint(user_with_no_records)
instead of print out :
"1234-4a20-47c0-b23c-a35a", "53dd-4120-4249-b4f6-ebe2"
it print out
"1","2","3","4","-","a","2","0"....
a.extend(b) is for extending list a by concatenating another sequence b onto it. When b is a string, and you force it to be interpreted as a sequence, it is interpreted as a sequence of individual characters. A simple example of this is:
>>> b = 'Hello'
>>> list(b)
['H', 'e', 'l', 'l', 'o']
Instead, you clearly want to do a.append(b), i.e. insert the entire string b as a single new item at the end of a.

Python: lists .remove(item) but not .find(item) or similar? [duplicate]

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

How to cat string with list objects? [duplicate]

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']

Categories

Resources