Prachi K.
asked 09/21/20How can I print these elements of the list in python?
The list is given [ 1, 2, [3, 4, 5], [6, 7]]. How can show the output as 1, 2, 3, 4, 5, 6, 7.
1 Expert Answer
Brian Z. answered 09/21/20
Python Software Engineer with 5+ Yrs Experience in Data Science
Hi
If the output should be 1 2 3 4 5 6 7 with no commas, as mentioned in your comment , the best way is tp make a loop over all items of the list with condition of the type, if it it is an integer display it with SPACE as end
or if the item is a list make another loop over its items as bellow:
ffor i in l:
if type(i)==int:
print(i, end=' ') # print the item if it is an integer with the end as space caracter
else: # if the item is a list make another loop over items of the list
for j in i:
print(j,end=' ')
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Prachi K.
The output should be 1 2 3 4 5 6 7 with no commas.09/21/20