1

I cant print out the second line

i tried to used divide by 2 and use two for loop to print it,

A=[1,2,3,4,5,6,7,8]
w=len(A)
T=w/2
for i in range(T):
  for ii in range(T):
    print A[ii]


A=[1,2,3,4,5,6,7,8]

i want to print like [1,2,3,4] and [5,6,7,8]

3 Answers 3

2

Using slicing:

A=[1,2,3,4,5,6,7,8]

print(A[:len(A)//2])                # print(A[:4])
print(A[len(A)//2:])                # print(A[4:])

OUTPUT:

[1, 2, 3, 4]
[5, 6, 7, 8]

EDIT:

For understanding;

A=[1,2,3,4,5,6,7,8]
w = len(A)

first_part = []
sec_part = []
count = 0                  # counter var to check for the first/sec half of list
for i in range((w)):
     if count < w//2:
          count += 1
          first_part.append(A[i])
     else:
      sec_part.append(A[i])

print(first_part)
print(sec_part)

OUTPUT:

[1, 2, 3, 4]
[5, 6, 7, 8]
Sign up to request clarification or add additional context in comments.

Comments

1

use list slicing:

A = [1,2,3,4,5,6,7,8]
print A[:len(A)/2]
print A[len(A)/2:]

Output would be:

[1,2,3,4]
[5,6,7,8]

Comments

0
print(A[:int(len(A)/2)], A[int(len(A)/2):])

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.