1

I want return a list with the string and index number horizontal. The output is wrong. Just like: 1 - Hello, 2 - World

Now I get a list

1
Hello
def add_index(str_list)

str_list = [1, 2, 3, 4, 5]
list_with_indexes = ["Hello", "World", "Assignment2", "Akash", "Kanhai"]

print ("(orginal list is)" + str(str_list))

print ("List index-value are : ")
s = ""
for i in range(len(str_list) - 0):
    s += str(str_list[i]) + " - " + (list_with_indexes[i]) + ", "

print (s)

return list_with_indexes

Aka
  • 19
  • 2

3 Answers3

0

First you should know, that you'll not get 1 - Hello, 2 - World, .. if your str_list contains [1,4,..]. Another point is, that your lists should have the same amonut of entry , otherwise you could receive an indexerror.

To produce your excepted output, you could write:

str_list = [1, 4, 5, 6]
list_with_indexes = ["Hello", "World", "This", "Is"]

print("(orginal list is)" + str(str_list))

print ("List index-value are : ")
for i in range(len(str_list)):
    print (str_list[i], '-', list_with_indexes[I])

that would return

(orginal list is)[1, 4, 5, 6]
List index-value are : 
1 - Hello
4 - World
5 - This
6 - Is

______ EDIT ______

you could also just index over the list elements like

list_with_indexes = ["Hello", "World", "This", "Is"]

print ("List index-value are : ")
for i in range(len(list_with_indexes)):
    print (i, '-', list_with_indexes[I])

gives you

1 - Hello
2 - World
3 - This
4 - Is

as python starts indexing with 0, you could write i+1 to start your output index with 1

    print (i+1, '-', list_with_indexes[I])

to get

0 - Hello
1 - World
2 - This
3 - Is

(because you asked for that,) you could also define a function processing that piece of code for you:

def listIndexing(list):
    print ("List index-value are : ")
    for i in range(len(list)):
        print (i, '-', list[i])

list_with_indexes = ["Hello", "World", "This", "Is"]

listIndexing(list = list_with_indexes)
Mig B
  • 637
  • 1
  • 11
  • 19
  • Thanks! So, I want to use the def function for this. Is this also possible? eq Def add_index(num_list) – Aka Jun 17 '19 at 09:36
0

If you need the output in one line then store it in a variable and then print it. Also, for loop range should be range(len(str_list) - 1). Consider the following:

str_list = [1, 4, 5, 6, 7]
list_with_indexes = ["Hello", "World", "This", "Is"]

print ("(orginal list is)" + str(str_list))

print ("List index-value are : ")
s = ""
for i in range(len(str_list) - 1):
    s += str(str_list[i]) + " - " + (list_with_indexes[i]) + ", "

print (s)
shrys
  • 5,860
  • 2
  • 21
  • 36
0

You can concatenate the strings to print all in the same line:

str_list = [1, 4, 5, 6, 7]
list_with_indexes = ["Hello", "World", "This", "Is"]

print "(orginal list is)" + str(str_list)

print ("List index-value are : ")
for i in range(len(str_list)):
    print str_list[i] + " - " + list_with_indexes[i]

Also you can make the loop joining both lists with zip:

str_list = [1, 4, 5, 6, 7]
list_with_indexes = ["Hello", "World", "This", "Is"]

print "(orginal list is)" + str(str_list)

print ("List index-value are : ")
for element in zip(str_list,list_with_indexes):
    print element[0] + " - " + element[1]
Shadowtrooper
  • 1,372
  • 15
  • 28