jay a
jay a

Reputation: 722

How do you convert a list of strings to separate strings in Python 3?

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.)

Upvotes: 0

Views: 79

Answers (4)

pmuntima
pmuntima

Reputation: 628

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

Upvotes: 0

Copperfield
Copperfield

Reputation: 8510

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

Upvotes: 0

9000
9000

Reputation: 40884

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

Upvotes: 2

slightlynybbled
slightlynybbled

Reputation: 2645

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!

Upvotes: 2

Related Questions