Apr-15-2019, 02:35 AM
(This post was last modified: Apr-16-2019, 01:21 AM by Drone4four.)
I’m trying to get slice a list of strings into individual characters ...but using slicing.
Here is my list:
But I am now trying to come up with an alternate solution this time using slicing. Here is my valiant attempt:
My humble lucid, cerebral explanation for that one-liner is: Python is printing the
I suppose a further and more important question that I have is this: Who among you could perform the same for loop operation that worked the first time but instead using two slices (as I have awkwardly attempted but failed to do)?
Here is my list:
first_list = ["apples", "bananas", "oranges",]Here is the end result and what I am trying to achieve:
['a', 'p', 'p', 'l', 'e', 's', 'b', 'a', 'n', 'a', 'n', 'a', 's', 'o', 'r', 'a', 'n', 'g', 'e', 's']I succeeded using a for loop:
for item in first_list: for character in item: second_list.append(character) print(second_list)That’s a basic for loop which I completely understand the syntax and logic.
But I am now trying to come up with an alternate solution this time using slicing. Here is my valiant attempt:
print(third_list.append(first_list[:][:]))My interpreter prints ‘None’ with that code. I am not sure why. Could someone explain?
My humble lucid, cerebral explanation for that one-liner is: Python is printing the
third_list
variable which is a composition of each letter (appended together) contained in first_list
. I’m obviously mistaken because the result is just None
. Could someone please correct my explanation here?I suppose a further and more important question that I have is this: Who among you could perform the same for loop operation that worked the first time but instead using two slices (as I have awkwardly attempted but failed to do)?