How do i print a list of elements side by side in python?

    j=0
    while j<5:
       random_lines=random.choice(men_heroes_lines)
       element=(random_lines.split(":")[0:1])
       random_lines_women=random.choice(women_heroes_lines)
       element_women=(random_lines_women.strip("\n").split(":")[0:1])

       print element
       print element_women
       j=j+1

hey guys here is my question.. i have two txt files which contains men and women names.. as:

manname1: a b c d
manname2: x y z e
...

i managed stripping to ":" but i can't write them side by side in a list..

expected output is=
["manname1","manname2","manname3"...]
however it is:
["manname1"]
["manname2"]
["manname3"]

how can i do it as in the expected output.. thanx in advance :)

this is my sample file..

 Ant Man: a, b
 Frodo: x,y
 Star : s, d
 Thor: r, t
 Spy: p,u

ant man,frodo,star... is the men mame.. and what i want to append a list..

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Printing a list in python can be done is following ways:

    • Using for loop : Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it. 

    Python

    a = [1, 2, 3, 4, 5]

    for x in range(len(a)):

        print a[x],

    • Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively. 

    Python

    a = [1, 2, 3, 4, 5]

    print(*a)

    print("printing lists separated by commas")

    print(*a, sep = ", "

    print("printing lists in new line")

    print(*a, sep = "\n")

    Output

    1 2 3 4 5
    printing lists separated by commas
    1, 2, 3, 4, 5
    printing lists in new line
    1
    2
    3
    4
    5
    

    • Convert a list to a string for display : If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string. 

    Python

    a =["Geeks", "for", "Geeks"]

    print(' '.join(a))

    a = [1, 2, 3, 4, 5]

    print str(a)[1:-1

    How do i print a list of elements side by side in python?

    Output

    Geeks for Geeks
    1, 2, 3, 4, 5
    

    • Using map : Use map() to convert each item in the list to a string if list is not a string, and then join them: 

    Python

    a = [1, 2, 3, 4, 5]

    print(' '.join(map(str, a))) 

    print"in new line"

    print('\n'.join(map(str, a)))

    Output

    1 2 3 4 5
    in new line
    1
    2
    3
    4
    5
    

    • Using list comprehension : Use list comprehension to go one by one to each element in list and print. 

    Python3

    a = [1, 2, 3, 4, 5]

    [print(i, end=' ') for i in a] 

    print("\nIn new line")

    [print(i) for i in a]

    Output

    1 2 3 4 5 
    In new line
    1
    2
    3
    4
    5
    


    How do you print elements side by side in Python?

    Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.

    How do you print lists next to each other in Python?

    You can use the zip() function to join lists together. The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments. Finally, just join() them together with newlines and you have the string you want.

    How do you print a list of elements on the same line in Python?

    When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.

    How do I print two lists of elements in Python?

    Method 2: Add two list using the Comprehension List.
    # initialize the Python lists..
    lt1 = [2, 4, 6, 8, 10, 30].
    lt2 = [2, 4, 6, 8, 10, 12].
    # print the original list element..
    print ( " Python list 1 : " + str (lt1)).
    print ( "Python list 2 : " + str (lt2)).
    # use list comprehension to add two lists..