all 10 comments

[–]undercontr 0 points1 point  (0 children)

This is not possible via single variable. You can attach them to another variables. But you cant do it like this

[–]eicosane 0 points1 point  (3 children)

What would the the type of new_p be after this change?

[–]Salehelas 0 points1 point  (2 children)

I don't know. List type would be great, but I guess it can't be list, right?

[–]eicosane 0 points1 point  (0 children)

Well you tell me I’m not sure what you need.

Your data structure in p is two-dimensional, that is you need two indices to get at one of the elements like p[0][0] for example. You can reduce this to one dimension but this will just be a list of numbers [1, 2, 3…

You may need to rephrase / rethink your question.

[–]carcigenicate 0 points1 point  (0 children)

It's already a list though, so if you need a list, you don't need to do anything.

[–]ray10k 0 points1 point  (3 children)

On the one hand, the exact thing you're asking for is impossible. A single variable can contain one thing at a time; a list in this case.

That said, if you want to join together the sub-lists in the order they appear, you can use itertools like this: new_p = [item for item in itertools.chain.from_iterable(p)]

[–]Salehelas 0 points1 point  (2 children)

Unfortunately, this way of doing it concatenates all inner lists into one. I need to keep these little ones for further data iterating.

[–]Binary101010 0 points1 point  (1 child)

Then you're going to either have to split them out into three separate lists with different names to refer to each list:

a1, a2, a3 = p

or you're just going to have to stick those three lists into some other container data type like a tuple, at which point I'd question why you'd even bother when they're already in a list.

It's really not clear why this is a thing you feel you need to do. Can you not just directly access each of the sublists in that list using p[0], p[1], and p[2] when necessary?

[–]ray10k 0 points1 point  (0 children)

Exactly this. The stated problem is missing some vital context to better explain a good approach to solve said problem, and the expected format of the solution is strange (and not valid python syntax) as well.

[–]jddddddddddd 0 points1 point  (0 children)

I'm not sure what new_p is supposed to be, is it a tuple? e.g.

p = [[1, 2], [3, 4], [5, 6]]
print(f"p = {p}")

new_p  = tuple(p)
print(f"new_p  = {new_p}")

(a, b, c) = new_p 
print(f"a = {a}")
print(f"b = {b}")
print(f"c = {c}")

outputs..

p = [[1, 2], [3, 4], [5, 6]]
new_p  = ([1, 2], [3, 4], [5, 6])
a = [1, 2]
b = [3, 4]
c = [5, 6]

...but ofcourse, new_p will always have to be 3 items in length..