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.
Related
This question already has answers here:
Access multiple elements of list knowing their index [duplicate]
(11 answers)
Closed 12 months ago.
So i want to take any integer as an input and get an output based on arrays like this:
Input: 012346
array = ["a","b","c","d","e","f","g"]
Output: abcdeg
how would i do that?
Use a comprehension. Convert input string to a list of characters then get the right element from array:
inp = '012346'
# For inp to be a string in the case of inp is an integer
out = ''.join([array[int(i)] for i in str(inp)])
print(out)
# Output
abcdeg
Update
How i would treat numbers above 10 since they would get broken down to 1 and 0
Suppose the following input:
inp = '1,10,2,3'
array = list('abcdefghijklmn')
out = ''.join([array[int(i)] for i in inp.split(',')])
print(out)
# Output
'bkcd'
Looks like operator.itemgetter could do the job.
>>> from operator import itemgetter
>>> itemgetter(0, 1, 2, 3, 4, 6)(array)
('a', 'b', 'c', 'd', 'e', 'g')
The input function in Python always returns a string, unless you cast it into another type. So you can loop over the digits of this string, cast the digits individually into an integer, and use that to fetch the letters from the list and concatenate them:
str = “”
data = input(“Data? “)
for digit in data:
str += array[int(digit)]
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:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Closed 2 years ago.
Why is the second print command giving an empty list while the first is giving proper output?
str1 = 'Hello'
str2 = reversed(str1)
print(list(str2))
print(list(str2))
Output:
['o', 'l', 'l', 'e', 'H']
[]
reversed is an iterator, iterators can be consumed only once meaning once you iterate over them, you cannot do it again.
Check the documentation
Cause for [] (empty list)
The built-in reversed, is an iterator, so it get exhausted once you have consumed it, by making a list. In your case, once you make it a do, list(revered("Hello")), it becomes, exhausted.
Solution
A quick solution could be making another iterator, code:
str1 = "Hello" # Original String
iter1 = reversed(str1) # Making the first iterator
iter2 = reversed(str1) # Making the second iterator
print(list(iter1), list(iter2)) # Printing the iterator once they are lists
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:
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']