0

I would like to print the right strings from a list I have, while looping through another list I have.

My two lists look like this:

overview_list = ['1','2','3']
my_list = ['name1', 'year1', 'date1', 'numbre1', 
'name2', 'year2', 'date2', 'numbre2',
'name3','year3','date3', 'numbre3']

I would like to loop through the overview_list and for every string I would like to print() the corresponding strings from my_list.

These are:

for '1' from overview_list it sould be printed: 'name1', 'year1', 'date1', 'numbre1'

for '2' from overview_list it should be printed: 'name2', 'year2', 'date2', 'numbre2'

and so on....

I think this could be done with a nested loop, but I am still a beginner in Python and do not know how. Thank you very much for your help!

2 Answers 2

1

try this, you don't need nested loop's instead using range(start, stop, step) + splicing

over_view = 3

for i in range(0, len(my_list), over_view + 1):
    print(", ".join(my_list[i: i + over_view + 1]))

name1, year1, date1, numbre1
name2, year2, date2, numbre2
name3, year3, date3, numbre3
Sign up to request clarification or add additional context in comments.

4 Comments

I am trying to generalise this therefore I choose overview_list = [1,2,3], but it could also be overview_list = [x,y,z]
@Etiende What are the values of [x,y,z] & how is your existing output going to change based on those values ?
x,y,z are just names as a string. For every name in overview_list the corresponding strings from my_list, which are name1, year1, date1, number1, for the first string on overview_list (here [x]). The corresponding strings from my_list for the secound string from overview_list (here [y]) would be name2, year2, date2, number2 and so on
@Etiende, So are you looking for some nested list to be created from my_list, [[name1,year1..], [name2, year2..]..]
0

If you would like to print every string in a different row:

overview_list = ['1','2','3']
my_list = ['name1', 'year1', 'date1', 'numbre1', 
'name2', 'year2', 'date2', 'numbre2',
'name3','year3','date3', 'numbre3']

for number in overview_list:
    for elem in my_list:
        if number in elem:
            print(elem)

Another option if you would like to print together all the strings that relate to one number:

overview_list = ['1','2','3']
my_list = ['name1', 'year1', 'date1', 'numbre1', 
'name2', 'year2', 'date2', 'numbre2',
'name3','year3','date3', 'numbre3']

for number in overview_list:
    for elem in my_list:
        if number in elem:
            print(elem, end=" ")
    print()

BTW you wrote numbre instead of number in 'my_list'

An addition after your comment: In order to use the ordinal number of any string in overview_list - try this:

overview_list = ['x','y','z']
my_list = ['name1', 'year1', 'date1', 'numbre1', 
'name2', 'year2', 'date2', 'numbre2',
'name3','year3','date3', 'numbre3']

for index, item in enumerate(overview_list, 1):
    for elem in my_list:
        if str(index) in elem:
            print(elem, end=" ")
    print() 

1 Comment

That works with overview_list = [1,2,3], but what could I do if overview_list = [x,y,z], but everything else remains the same.

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.