I've been working on a code which will flip the order of elements backwards using 2 functions, for some reason the first function which can normally print out two or more lists with spaces between each element is putting a space between each letter of the flipped list, could you tell me why it's doing so
cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]
def printList(nightwatch):
for r in nightwatch:
for c in r:
print(c, end=' ')
def flipOrder(listy):
temp = []
for r in range (len(listy)):
for c in range (len(listy[r])):
temp.insert (0, listy[r][c])
listy = temp
printList(listy)
printList(cities)
print ()
print ()
print ()
flipOrder(cities)
I've tried implementing the first function into the second as well as messing with the c and r loop, however this usually just causes the code to stop working, ideally i'd like to know why this is processing the list one letter at a time
I've been working on a code which will flip the order of elements backwards using 2 functions, for some reason the first function which can normally print out two or more lists with spaces between each element is putting a space between each letter of the flipped list, could you tell me why it's doing so
cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]
def printList(nightwatch):
for r in nightwatch:
for c in r:
print(c, end=' ')
def flipOrder(listy):
temp = []
for r in range (len(listy)):
for c in range (len(listy[r])):
temp.insert (0, listy[r][c])
listy = temp
printList(listy)
printList(cities)
print ()
print ()
print ()
flipOrder(cities)
I've tried implementing the first function into the second as well as messing with the c and r loop, however this usually just causes the code to stop working, ideally i'd like to know why this is processing the list one letter at a time
Share Improve this question asked Feb 7 at 17:17 formentera ladyformentera lady 1 New contributor formentera lady is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1 |2 Answers
Reset to default 1Your printList
function is double nested to deal with the two subLists in the cities
List. But fliporder
creates a single List (no-subs) so the nested loop in printList
takes each character each time.
Here is a version which flips the order within each sublist then flips the sublists. It used a List comprehension for simplicity.
cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]
def printList(nightwatch):
for r in nightwatch:
for c in r:
print(c, end=' ')
def flipOrder(listy):
listy = [x[::-1] for x in listy[::-1]]
printList(listy)
printList(cities)
print ()
print ()
print ()
flipOrder(cities)
which gives:
Miami Atlanta Dallas Los Angeles New York Chicago Portland Sacramento
Sacramento Portland Chicago New York Los Angeles Dallas Atlanta Miami
The following line of code already iterates through the merged list named nightwatch
['Sacramento', 'Portland', 'Chicago', 'New York', 'Los Angeles', 'Dallas', 'Atlanta', 'Miami']
(Your flipOrder
function merges both sublists to one):
for r in nightwatch:
...
Therefore, you don't need this:
for c in r:
...
Since it just iterates through every single character and adds a space to it.
This code should work:
cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]
def printList(nightwatch):
for r in nightwatch:
print(r, end=' ')
def flipOrder(listy):
temp = []
for r in range (len(listy)):
for c in range (len(listy[r])):
temp.insert (0, listy[r][c])
listy = temp
printList(listy)
printList(cities)
print ()
print ()
print ()
flipOrder(cities)
flipOrder
function is rather "unpythonic." Better would befor group in list: for city in group: temp.insert(0, city)
– Anerdw Commented Feb 7 at 17:59