Pages

Wednesday, August 14, 2013

Merge two lists in Python

When you merge two lists with Python, you can use the overloaded '+' operator.
a = [1,2,3]
b = [2,3,4]

>>> a + b
[1, 2, 3, 2, 3, 4]
However, when you don't want the duplicate values, you need to use set. '+' operator is not supported in a set, so use union() method.
>>> set(a).union(b)
set([1, 2, 3, 4])
You can convert a set to list with list() method.
>>> list(set(a).union(b))
[1, 2, 3, 4]
Refer to set in python document.

32 comments:

  1. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    Angularjs training in chennai

    ReplyDelete
  2. All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
    Click here:
    python training in Bangalore
    Click here:
    python training in Bangalore

    ReplyDelete
  3. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    python training in pune | python training institute in chennai | python training in Bangalore

    ReplyDelete

  4. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.

    Amazon Web Services Training in Pune | Best AWS Training in Pune


    AWS Training in Pune | Best Amazon Web Services Training in Pune

    ReplyDelete
  5. I’m bookmarking and will be tweeting this to my followers! Wonderful blog and amazing design and style
    nebosh course in chennai

    ReplyDelete
  6. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favourites blog site list and will be checking back soon.

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  7. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  8. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Data science Course Training in Chennai | Data Science Training in Chennai
    RPA Course Training in Chennai | RPA Training in Chennai
    AWS Course Training in Chennai | AWS Training in Chennai

    ReplyDelete
  9. Informative Post! Python Training in Pune is the fastest growing and most popular programming language course learnt by Data Analysts.

    ReplyDelete
  10. Thank you for sharing such a nice and interesting blog with us regarding Java. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  11. Hi buddies, it is great written piece entirely defined, continue the good work constantly.
    Data Science Course in Bangalore

    ReplyDelete
  12. Good day! I could have sworn I’ve visited this web site before but after browsing through many of the posts I realized it’s new to me. Regardless, I’m definitely happy I discovered it and I’ll be bookmarking it and checking back often!
    Technology

    ReplyDelete
  13. This is a topic that is close to my heart... Take care! Where are your contact details though?
    Technology

    ReplyDelete
  14. Honestly speaking this blog is absolutely amazing in learning the subject that is building up the knowledge of every individual and enlarging to develop the skills which can be applied in to practical one. Finally, thanking the blogger to launch more further too.

    Data Analytics Course in bhilai

    ReplyDelete
  15. This post is so interactive and informative.keep update more information...
    IELTS Coaching in Tambaram
    IELTS Coaching in Chennai

    ReplyDelete
  16. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one.
    business analytics course in hyderabad

    ReplyDelete
  17. Great post. keep sharing such a worthy information.
    Hadoop Training in Chennai

    ReplyDelete

  18. This post is so useful and informative. Keep updating with more information.....
    Best Spoken English Classes in Bangalore
    Spoken English in Bangalore

    ReplyDelete
  19. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.
    data science training in hyderabad

    ReplyDelete
  20. I think this is a really good article. You make this information interesting and engaging. You give readers a lot to think about and I appreciate that kind of writing.
    data science training

    ReplyDelete
  21. MS enterprises 2007 for free with product key the complete setup file with Word, Outlook, PowerPoint, Excel, Access, OneNote. thank you for the blog proclaim buddy! maintain them coming... Microsoft Office 2007 Free Download Crack Full Version 64 Bit

    ReplyDelete
  22. Even if HandBrake is available for both Windows 32 and 64bit platforms, the HandBrake latest version is only available in 64-bit and therefore Free Download HandBrake

    ReplyDelete