0

I wrote this program to join all the elements of sub-lists in a single result empty list and then sort the returned result list. But after i run this program i get a unusual output. I get None on console screen. What's wrong here in this code?

n=[[44,45,76,8,678,24,24],[45,45737,45,7373]]

def makelist(lis):
     result= []
     for i in lis:
         for j in i:
           result.append(j)
     return result

print makelist(n).sort()
3
  • Is your indentation correct here? It looks like you should indent the inner loop Commented Jul 31, 2014 at 9:15
  • sorry edited that was typing problem @EdChum Commented Jul 31, 2014 at 9:17
  • If you changed your last line to n = makelist(n) n.sort() print n it should work Commented Jul 31, 2014 at 9:22

6 Answers 6

2
n=[[44,45,76,8,678,24,24],[45,45737,45,7373]]

def makelist(lis):
 result= []
 for i in lis:
     for j in i:
       result.append(j)
 return result

All this is OK. Now run:

y = makelist(n)
y.sort()
print y

Your list is sorted and stored in the 'y' variable.

Sign up to request clarification or add additional context in comments.

Comments

2

makelist(n).sort() does not return anything

Do it like

temp = makelist(n)
temp.sort()
print temp

Comments

2

Given a list lst and then calling sort will sort the list in-place, but it wiil not return the sorted list.

An example solution:

# Make this work in Python2 and Python3
from __future__ import print_function

import itertools


n=[[44,45,76,8,678,24,24],[45,45737,45,7373]]

def makelist(lis):
     result= []
     for i in lis:
         for j in i:
           result.append(j)
     return result

# Variation 1
print(sorted(makelist(n)))

# Variation 2
lst = makelist(n)
lst.sort()
print(lst)

# Variation 3 (replacing the makelist flattening operation)
lst = list(itertools.chain.from_iterable(n))
lst.sort()
print(lst)

4 Comments

can you explain you code? What is this future? makelist() itertools.chain.from_iteratable(n)? This code really looks typical to me
The line with future turns print into a funktion also in Python2. It enables the code to run in both version 2 and 3.
The module itertools is a handy standard library and chain.from_iterable(iterable) (returns a chain objekt. It is an alternate chain() contructor taking a single iterable argument that evaluates lazily. When this is passed to list a new and flattended list is created replacing your makelist function.
So in summary, Variation 1 and 2 solves your problem and Variation 3 solves it in a different way and the __future__ line makes it reasonably version agnostic.
1

Use sorted method instead of sort and this will fix your problem:

n=[[44,45,76,8,678,24,24],[45,45737,45,7373]]

def makelist(lis):
     result= []
     for i in lis:
         for j in i:
           result.append(j)
     return result

print sorted(makelist(n))

# OR check this way
res = makelist(n)
print res
res.sort()
print res

To read more about sort() you can use the following link: http://www.tutorialspoint.com/python/list_sort.htm

Comments

1

sort() is a method of list. It sorts the list in place and does not return the list as a reminder of that fact.

sorted() is a builtin function, not a method on list, because it's more general taking any iterator as its first argument, not just a list. It of course does return a list.

n=[[44,45,76,8,678,24,24],[45,45737,45,7373]]

    def makelist(lis):
         result= []
         for i in lis:
             for j in i:
               result.append(j)
         return result
    print sorted(makelist(n))

The .sort() method of lists sorts the list in place, while sorted() creates a new list.

Comments

1

sort() function sorts in-place and returns None. On the other hand, sorted() function returns a new list. Thus, you can use your current implementation:

makelist(n).sort()
print(n)

Or, you can use sorted():

print sorted(makelist(n), key=int)  

Comments

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.