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
#6
(Apr-17-2019, 12:35 AM)Drone4four Wrote: I’m not sure I understand what you mean when you say “consume the iterator”. You explained that print(list(flat_list)) wouldn’t work because the last was supposed to be already exhausted. But I entered each line into my Python interpreter and there was no traceback or issue presented.

There is subtle difference in here:

In [8]: from itertools import chain                                                                                             

In [9]: flat_list = chain.from_iterable(['apples', 'bananas', 'oranges'])                                                       

In [10]: print(flat_list)                    # printing out object, not consuming iterator                                                                                
<itertools.chain object at 0x10cd56048>       

In [11]: list(flat_list)                     # consuming iterator i.e. iterating over and creating list                                                                                  
Out[11]: 
['a', /../  's']

In [12]: list(flat_list)                     # consuming exhausted iterator                                                                                                                 
Out[12]: []

(Apr-15-2019, 02:35 AM)Drone4four Wrote: I’m trying to get slice a list of strings into individual characters ...but using slicing.

I observed that there is no answer to original question. I have no idea why would anyone want to do it as there are built-in tools for that. However, following silly code uses slicing as aftertought to get list of individual characters:

 
>>> lst = ['apples', 'bananas', 'oranges']
>>> [fruit[i:i+1] for fruit in lst for i in range(len(fruit))]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Slicing Python list of strings into individual characters - by perfringo - Apr-17-2019, 07:22 AM

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