5 Replies - 192 Views - Last Post: 06 March 2019 - 06:01 AM Rate Topic: -----

#1 sx200n   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 07-July 17

Producing a single list from several lists

Posted 06 March 2019 - 02:33 AM

Hello,

I seem to have hit a mental block on an assignment I have.

Basically I have a set of nested lists with each list containing words of an increasing number of characters. So currently we have a list containing four words [help,me,with,it]. And we have been tasked with placing each word in the right list. So the first list is empty as there are not words with 1 character, the second contains both 'me' and ;it' as they have two characters, etc. This is achieved using the following code:
for words in words:
    newList[len(word) - 1.append(word)



Nice and simple, and it words no matter what list of words I pass it.

I now have to put all of these lists back together as a single list of words as the above function would have sorted them into a different order. However whenever I do this, I seem to still get nested lists and I need to have just a single list.

It is probably something very simple that I am not doing, but I have tried this...
finalList = []
for entry in newList:
    if entry != []:
        finalList.append(entry)



The final result is correct in that it gives me the words in the expected order, but they are still within nested lists. So I get [[it, me],[help,with]], but I need to get [it, me, help, with].

Any help would be greatly appreciated.

Any pointers in what I am doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Producing a single list from several lists

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 513
  • View blog
  • Posts: 1,632
  • Joined: 27-December 13

Re: Producing a single list from several lists

Posted 06 March 2019 - 03:22 AM

You need to concatenate the 'finalList', like this:
finalList = []
for entry in newList:
    if entry != []:
        finalList += entry

Was This Post Helpful? 0
  • +
  • -

#3 sx200n   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 07-July 17

Re: Producing a single list from several lists

Posted 06 March 2019 - 03:29 AM

dk3250

That now works.

Thanks for your help, I knew it would be something simple I was missing.
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 513
  • View blog
  • Posts: 1,632
  • Joined: 27-December 13

Re: Producing a single list from several lists

Posted 06 March 2019 - 03:33 AM

Good.
Just for the record, your first code snippet is wrong, should be:
for word in words:
#       ^ no 's'
    newList[len(word) - 1].append(word)
    #                    ^ end bracket was missing


Was This Post Helpful? 0
  • +
  • -

#5 sx200n   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 07-July 17

Re: Producing a single list from several lists

Posted 06 March 2019 - 03:56 AM

Ah yes, I blame the rubbish keyboard I have to use at work :whistling:
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: Producing a single list from several lists

Posted 06 March 2019 - 06:01 AM

If they won't give you a new one then I would invest a few dollars in one myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1