Python Forum
Slicing Python list of strings into individual characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slicing Python list of strings into individual characters
#1
I’m trying to get slice a list of strings into individual characters ...but using slicing.

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)?
Reply


Messages In This Thread
Slicing Python list of strings into individual characters - by Drone4four - Apr-15-2019, 02:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 480 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 768 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 710 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,547 May-01-2023, 09:06 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,092 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  How to expand and collapse individual parts of the code in Atom Lora 2 1,164 Oct-06-2022, 07:32 AM
Last Post: Lora
  Splitting strings in list of strings jesse68 3 1,777 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Extract continuous numeric characters from a string in Python Robotguy 2 2,650 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,832 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  slicing and indexing a list example leodavinci1990 4 2,354 Oct-12-2020, 06:39 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020