0

I want to know if you have a list of strings such as:

l = ['ACGAAAG', 'CAGAAGC', 'ACCTGTT']

How do you convert it to:

O = 'ACGAAAG'
P = 'CAGAAGC'
Q = 'ACCTGTT'

Can you do this without knowing the number of items in a list? You have to store them as variables. (The variables don't matter.)

4
  • Wanting this is almost always a sign you've taken a wrong step. Say you start with a list of 1,000 elements and make 1,000 separate names. Then what? Are you going to write 1,000 lines of code every time you want to do the same thing to everything? What advantage do you think this will give you over just using a list or a dictionary? Commented Feb 5, 2017 at 1:05
  • True, so can you make a list of strings into a list of lists? Commented Feb 5, 2017 at 1:18
  • @jaya sure you can, there are plenty of ways, it depend on how you want it... Commented Feb 5, 2017 at 1:38
  • I you don't know how many items you will have, then you want them in a list. What is it you are trying to do, there will be a better solution than unpacking each item. Commented Feb 5, 2017 at 5:43

4 Answers 4

2

Welcome to SE!

Structure Known

If you know the structure of the string, then you might simply unpack it:

O, P, Q = my_list

Structure Unknown

Unpack your list using a for loop. Do your work on each string inside the loop. For the below, I am simply printing each one:

for element in l:
    print(element)

Good luck!

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, but do you know a way that you don't know the number of items in the list?
I added additional method for your unknown structure. Please add more detail about the final desired structure if this isn't enough.
you don't need to transform it to a tuple to unpack it
I see! I have only unpacked tuples in the past, so that jaded the answer. Thanks for the info! Updated.
But can you store them as variables?
|
2

If you don't know the number of items beforehand, a list is the right structure to keep the items in.

You can, though, cut off fist few known items, and leave the unknown tail as a list:

a, b, *rest = ["ay", "bee", "see", "what", "remains"]
print("%r, %r, rest is %r" % (a, b, rest))

Comments

0
a,b,c = my_list

this will work as long as the numbers of elements in the list is equal to the numbers of variables you want to unpack, it actually work with any iterable, tuple, list, set, etc

if the list is longer you can always access the first 3 elements if that is what you want

a = my_list[0] 
b = my_list[1]
c = my_list[2]

or in one line

a, b, c = my_list[0], my_list[1], my_list[2]

even better with the slice notation you can get a sub list of the right with the first 3 elements

a, b, c = my_list[:3]

those would work as long as the list is at least of size 3, or the numbers of variables you want

you can also use the extended unpack notation

a, b, c, *the_rest = my_list

the rest would be a list with everything else in the list other than the first 3 elements and again the list need to be of size 3 or more

And that pretty much cover all the ways to extract a certain numbers of items

Now depending of what you are going to do with those, you may be better with a regular loop

for item in my_list:
    #do something with the current item, like printing it
    print(item)

in each iteration item would take the value of one element in the list for you to do what you need to do one item at the time


if what you want is take 3 items at the time in each iteration, there are several way to do it

like for example

for i in range(3,len(my_list),3)
    a,b,c = my_list[i-3:i]
    print(a,b,c)

there are more fun construct like

it = [iter(my_list)]*3
for a,b,c in zip(*it):
    print(a,b,c)

and other with the itertools module.

But now you said something interesting "so that every term is assigned to a variable" that is the wrong approach, you don't want an unknown number of variables running around that get messy very fast, you work with the list, if you want to do some work with each element it there are plenty of ways of doing it like list comprehension

my_new_list = [ some_fun(x) for x in my_list ]

or in the old way

my_new_list = []
for x in my_list:
    my_new_list.append( some_fun(x) )

or if you need to work with more that 1 item at the time, combine that with some of the above

7 Comments

How do you make: ['a', 'b', 'c'...] into a = 'a', b = 'b', c = 'c'... until all units are covered, without knowing the length? All items have to be converted to a string.
you mean taking 3 items at the time in each iteration?
Just so that all items in a list are converted so that every term is assigned to a variable.
you don't want to that... what is what you really want? also check my update
I want to be able to compare characters of strings in a list. Basically, I want to see in the list ['ab', 'ac'] I want to see if 'a' = 'a' (the first terms ) and 'b' = 'c' (the second terms).
|
0

I do not know if your use case requires the strings to be stored in different variables. It usually is a bad idea.

But if you do need it, then you can use exec builtin which takes the string representation of a python statement and executes it.

list_of_strings = ['ACGAAAG', 'CAGAAGC', 'ACCTGTT']

Dynamically generate variable names equivalent to the column names in an excel sheet. (A,B,C....Z,AA,AB........,AAA....)

variable_names = ['A', 'B', 'C'] in this specific case

for vn, st in zip(variable_names, list_of_strings):
        exec('{} = "{}"'.format(vn, st))

Test it out, print(A,B,C) will output the three strings and you can use A,B and C as variables in the rest of the program

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.