Python Forum
[Help] How to print elements from list of lists?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] How to print elements from list of lists?
#1
Hi,

I'm trying to print the elements inside the list world. I'm using for loop but I kept getting "SyntaxError: invalid character in identifier"

The output should be like:
water water water water water water water water water water water
water water water water land land water water water water water
etc....

[Image: 1208_zpszuo4p7l7.png]
==============================================================================
Here's the exercise:
# Write a function that prints out all elements of the above board, starting from the first element of the first line, till the end. Each line should be read from beginning to end.
# Now write a function that prints out all elements in reverse.
==============================================================================
M = ​"land"
o = ​"water"
world = [
		 [o,o,o,o,o,o,o,o,o,o,o],
		 [o,o,o,o,M,M,o,o,o,o,o],
		 [o,o,o,o,o,o,o,o,M,M,o],
		 [o,o,o,M,o,o,o,o,o,M,o],
		 [o,o,o,M,o,M,M,o,o,o,o],
		 [o,o,o,o,M,M,M,M,o,o,o],
		 [o,o,o,M,M,M,M,M,M,M,o],
		 [o,o,o,M,M,o,M,M,M,o,o],
		 [o,o,o,o,o,o,M,M,o,o,o],
		 [o,M,o,o,o,M,o,o,o,o,o],
		 [o,o,o,o,o,o,o,o,o,o,o]
		]
for i in world:
	print(",".join(*i).split())
Thank you.
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply
#2
M = "land"
o = "water"
world = [
         [o,o,o,o,o,o,o,o,o,o,o],
         [o,o,o,o,M,M,o,o,o,o,o],
         [o,o,o,o,o,o,o,o,M,M,o],
         [o,o,o,M,o,o,o,o,o,M,o],
         [o,o,o,M,o,M,M,o,o,o,o],
         [o,o,o,o,M,M,M,M,o,o,o],
         [o,o,o,M,M,M,M,M,M,M,o],
         [o,o,o,M,M,o,M,M,M,o,o],
         [o,o,o,o,o,o,M,M,o,o,o],
         [o,M,o,o,o,M,o,o,o,o,o],
         [o,o,o,o,o,o,o,o,o,o,o]
        ]
for i in world:
#    print(",".join(i).split('\n'))
    print(",".join(i))
Output:
water,water,water,water,water,water,water,water,water,water,water water,water,water,water,land,land,water,water,water,water,water water,water,water,water,water,water,water,water,land,land,water water,water,water,land,water,water,water,water,water,land,water water,water,water,land,water,land,land,water,water,water,water water,water,water,water,land,land,land,land,water,water,water water,water,water,land,land,land,land,land,land,land,water water,water,water,land,land,water,land,land,land,water,water water,water,water,water,water,water,land,land,water,water,water water,land,water,water,water,land,water,water,water,water,water water,water,water,water,water,water,water,water,water,water,water
Reply
#3
[Image: 1208_zpsh2pjjnu2.png]

==============================================================================
M = "land"
o = "water"
world = [
		 [o,o,o,o,o,o,o,o,o,o,o],
		 [o,o,o,o,M,M,o,o,o,o,o],
		 [o,o,o,o,o,o,o,o,M,M,o],
		 [o,o,o,M,o,o,o,o,o,M,o],
		 [o,o,o,M,o,M,M,o,o,o,o],
		 [o,o,o,o,M,M,M,M,o,o,o],
		 [o,o,o,M,M,M,M,M,M,M,o],
		 [o,o,o,M,M,o,M,M,M,o,o],
		 [o,o,o,o,o,o,M,M,o,o,o],
		 [o,M,o,o,o,M,o,o,o,o,o],
		 [o,o,o,o,o,o,o,o,o,o,o]
		]
print ("\t\tTRAVERSE\n")
for i in world:
	print(",".join(i))

print ("\t\tREVERSE\n")
for i in reversed(world):
	print(",".join(i))
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 443 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  problem with print lists MarekGwozdz 4 687 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 481 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  How do you get Python to print just one value in a list? 357mag 3 1,015 May-17-2023, 09:52 PM
Last Post: rob101
  List all possibilities of a nested-list by flattened lists sparkt 1 921 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 1,108 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,073 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,068 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,638 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to change the datatype of list elements? mHosseinDS86 9 2,000 Aug-24-2022, 05:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020