-1

I have a nested tuple t1 = ((1, 'Kamil'), (2, 'Hassaan')) and I want to copy the elements of the tuple into a list like this: [[1, 2], [Kamil, Hassaan]]. Take 1 and 2 and combine them. Take Kamil and Hassaan and combine them. Combine them together in a small list(temp) and append temp in the List.

This is my code:

t1 = ((1, 'Kamil'), 
      (2, 'Hassaan'))

t2 = ((1, 'python', 'print'),
      (2, 'c++', 'cout'))
iSize = len(t1[0])
#print(len(t1))

index = 0
List = []
temp = []

r = 0
c = 0

while r < len(t1[0]):
    while c < len(t1):

        temp.append(t1[c][r])
        c += 1

    List.append(temp)
    print(List)

    c = 0
    temp.clear()
    print(temp)
    r += 1

print(List)

You will see some unnecessary print commands. I used them to check my code. I can't understand why after one iteration the items of the List get overwritten and then the whole List is empty at the end.

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 1
    Possible duplicate of [Transpose list of lists](https://stackoverflow.com/questions/6473679/transpose-list-of-lists) – Georgy Mar 26 '19 at 14:59
  • @Georgy I was unable to find the link you have provided, but, I am trying to implement my own logic. And the output of the most rated answer is giving an incorrect output and a few more answers in the post have given an incorrect output. So I would rather stick to what I wrote and improve that – Kamil Hassaan Mar 27 '19 at 16:17
  • I don't understand why you find them incorrect. Running `list(map(list, zip(*t1)))` on your `t1` gives exactly the result that you wanted. – Georgy Mar 27 '19 at 16:21
  • I checked it again and I found out that I was executing incorrectly – Kamil Hassaan Mar 27 '19 at 16:28

3 Answers3

1

When you call temp.clear() you are clearing both the temporary variable AND what you just appended to the list. One way around this is to use copy from the copy module to make a new variable to append to the list, then clearing the temp variable has to effect on your final list.

Code:

from copy import copy

t1 = ((1, 'Kamil'), 
      (2, 'Hassaan'))

t2 = ((1, 'python', 'print'),
      (2, 'c++', 'cout'))
iSize = len(t1[0])
#print(len(t1))

index = 0
List = []
temp = []

r = 0
c = 0

while r < len(t1[0]):
    while c < len(t1):

        temp.append(t1[c][r])
        c += 1

    List.append(copy(temp))
    print(List)

    c = 0
    temp.clear()
    print(temp)
    r += 1

print(List)
Hoog
  • 2,280
  • 1
  • 14
  • 20
1

Overall the logic is relatively simple but can be confusing, and this is partially due to naming conventions. It is best to avoid naming variables with class names (in this case List), you can check Pep-8 for coding standards.

Further to this, the while loop seems to over-complicate a little the output. You can simplify with nested for-loops:

master_list = [[] for element in range(len(t1[0]))]
for inner_tuple in t1:
    for i in range(len(inner_tuple)):
        master_list[i].append(inner_tuple[i])

Hope this helps!

nickyfot
  • 1,932
  • 17
  • 25
0

I'm not sure what the entire process is that you want, but if the result is to have two lists within a list, separated by the type of data (alphabets and numbers) then you can try this:

t1 = ((1, 'Kamil'), (2, 'Hassaan'))

list_one = []
list_two = []
result = []
for i in t1:
    list_one.append(i[0])
    list_two.append(i[1])

result.append(list_one)
result.append(list_two)
print(result)
Onengiye Richard
  • 348
  • 6
  • 12