How to assign the elements of a list to different variables in Python?

Asked

Viewed 540 times

0

I have a list, like the following:

lista = [1, 2, 3, 4, 5]

Even without knowing how many elements there are inside the list, how do I assign each element of the list to a distinct variable? (as in the example below)

elemen1 = '1'
elemen2 = '2'
elemen3 = '3'
elemen4 = '4'
elemen5 = '5'
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

3

This?

lista = [1, 2, 3, 4, 5]
a, b, c, d, e = lista

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can do it with the tuple mechanism, where you can define variables in sequence and the list will automatically population them. Of course, you need to match the number of elements with the number of variables, otherwise some elements will not enter the variables or some variables will be worthless.

This almost always makes no sense and does not bring advantages. A variable accessed by your index is still a variable. Especially a variable like the same differentiated name only a sequential number shows that it is the wrong option. In very few cases where an API works one way and there is a lot of advantage in another way is that it should do this.

  • The code cited would not work, because I want to do without knowing how many elements have in the list

  • 1

    There you want the impossible (within normal range) or completely unnecessary, as I mentioned. I actually answered to indicate curiosity, with this comment it is clear that you want to do something meaningless. It is even possible to use reflection, but then it makes even less sense, because the only justification to do this is a possible tiny performance improvement, which depends even on implementation and could not even occur. With reflection it will be much slower always, so the only hypothetical motivation would end. And it still wouldn’t make sense to access variables that you don’t even know exist

  • I get it. I’m sorry if I offended you. It’s a little knowledge of the subject. I could access the list elements by the index, yet I want to do a code that, in this new case, counts the amount of list elements, and does the next pipeline for each element of the list. That is, I do not want to know how many elements you have on the list, to later perform the functions for each of them.

  • 2

    No offense, I can’t imagine how. What you really want is something else and it becomes clearer that you don’t need isolated variables, you just want to access the list. I answered what you asked, which serves almost just as curiosity of how it works.

  • 2

    @Brunosantos To "perform functions on each element" you use one for and does not need loose variables. Something like for elemento in lista: funcao(elemento). In the end it is proving exactly what Maniero said, that your example is not what you want and the loose variables do not help only unaided.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.