So I have a list plaintext
that contains ['A', 'A', 'R', 'O', 'N']
and I want to end up with a set of lists called letter1
, letter2
, letter3
, and so on, that contain ['A']
, ['A']
, ['R']
, and so on. How do I go about doing this without cloning the list five times and removing the extra parts?
Asked
Active
Viewed 36 times
-1

ohyeah2389
- 25
- 4
-
What have you already tried? SO won't write your code for you. If you've tried something, _edit your question_ to include your code. If not... well, go and try something! :P – Sebastian Lenartowicz Aug 26 '16 at 00:55
-
@SebastianLenartowicz SO might not, but somebody did :P – Mark Tolonen Aug 26 '16 at 01:35
-
I know you won't write code for me, I just wanted to know if there was any function like this. – ohyeah2389 Aug 26 '16 at 04:28
-
`[[e] for e in ['A', 'A', 'R', 'O', 'N']]` – dawg Aug 26 '16 at 04:33
-
That's not what I was looking for. I want to know how to have each sublist that that code makes in `plaintext` and have them as a completely new list, that can be referenced with: `letter1 letter2` and so on. – ohyeah2389 Aug 26 '16 at 04:37
-
Oh, and by the way, `plaintext` could be anything, of any length. I just had AARON as a suggestion. – ohyeah2389 Aug 26 '16 at 04:38
1 Answers
1
You can iterate over the list:
In [1]: letters = ['A', 'A', 'R', 'O', 'N']
#use list comprehension to iterate over the list and place each element into a list
In [2]: [[l] for l in letters]
Out[2]: [['A'], ['A'], ['R'], ['O'], ['N']]
To add titles, we typically use a dictionary. For example
#create a dictionary
letters_dict = {}
#iterate over original list as above except now saving to a dictionary
for i in range(len(letters)):
letters_dict['letter'+str(i+1)] = [letters[i]]
This gives you the following:
In [4]: letters_dict
Out[4]:
{'letter1': ['A'],
'letter2': ['A'],
'letter3': ['R'],
'letter4': ['O'],
'letter5': ['N']}
You can now access each of the lists as follows:
In [5]: letters_dict['letters1']
Out[5]: ['A']
Finally, just for completeness, there's a cool extension of the dictionary method. Namely, using code from this thread, you can do the following:
#create a class
class atdict(dict):
__getattr__= dict.__getitem__
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
#create an instance of the class using our dictionary:
l = atdict(letters_dict)
This way, you can do the following:
In [11]: l.letter1
Out[11]: ['A']
In [12]: l.letter5
Out[12]: ['N']
If you have no desire to store the values in an iterable or referencable object (ie dictionary, list, class) as you suggest in your question, then you could literally do the below:
letter1 = letters[0]
letter2 = letters[1]
letter3 = letters[2]
#and so forth ...
but as you can see, even with 6 variables the above becomes tedious.

Community
- 1
- 1

Gene Burinsky
- 9,478
- 2
- 21
- 28
-
Maybe something like this would work. Can i `letters1 = letters_dict['letters0']`? – ohyeah2389 Aug 26 '16 at 04:32
-
1You want `[[l] for l in letters]`, not `[list(l) for l in letters]`; the latter works as expected by coincidence (because length one strings are iterables that produce themselves), but for length != 1 strings, you'd end up with multielement lists by using `list`. – ShadowRanger Aug 26 '16 at 04:34
-
Ok then. If I happen to use something similar to this, I'll make sure to change that. – ohyeah2389 Aug 26 '16 at 04:40
-
@ohyeah2389, I'm not quite sure I understand what you're trying to achieve, are you literally suggesting creating a new variable for each letter and assigning a list to that variable? I mean, by all means but that's most commonly impractical for subsequent tasks – Gene Burinsky Aug 26 '16 at 05:02
-
I need to separate a list containing items which are single letters. I then need to scramble those letters in a way where they can later be read and unscrambled. And I'm a noob at python. – ohyeah2389 Aug 26 '16 at 05:10
-
@ohyeah2389, could you edit your question by adding an example of the scrambling/unscrambling task you wish to have done? – Gene Burinsky Aug 26 '16 at 05:13
-
No, because it's irrelevant, and if I need help with it, I can ask another question. – ohyeah2389 Aug 26 '16 at 05:15
-
-