Home > OS >  List item extraction in Python
List item extraction in Python

Time:10-30

If I have list = [1, [a,b]], how can I make split the list item in list to become list = [1, a], [1,b]?

CodePudding user response:

if your list stays like list = [1, [a,b]] can go for a not so complex approach

a='a'
b='b'
list = [1, [a,b]]

list = [[list[0],list[1][0]],[list[0],list[1][1]]]

if your list can be something like list = [1, [a,b,c,d,.....]] you can go for an iterative approach

a='a'
b='b'
list = [1, [a,b]]

list = [[list[0],x] for x in list[1]]

CodePudding user response:

You can first create a list then another. then run nested for loops to individually print out each component of list2 each component of list 1. I spent a lot of time in this, so hope this has helped you out:

list1 = ["a", "b"]
list2 = [1, list1]

for i in list2[:-1]:
    for j in list1:
        print(str(i)   str(j))

If it helped, I would wish to get an 'accepted' mark!

  • Related