How to convert Python Multiple list to list

A forum for general discussion of the Python programming language.

How to convert Python Multiple list to list

Postby priyanka_hdp » Wed May 15, 2013 10:51 am

here is my python list when i debug it

Code: Select all
    list: [[4, 1, False], [4, 2, False], [4, 3, False], [4, 4, False], [4, 5, False]]


now i need to convert it to this type list

Code: Select all
    list: [1,2,3,4,5]


please advice me to implement this.
priyanka_hdp
 
Posts: 5
Joined: Thu Mar 28, 2013 10:35 am

Re: How to convert Python Multiple list to list

Postby Mekire » Wed May 15, 2013 11:03 am

Huh? You want the second element of each sublist in one list? Is that it?
Create a new empty list. Iterate through your first list with a for loop and append the item at index 1 to the list each time through.

I would personally do it with a list comprehension but you should try it with a basic for loop first.

Confirm this is what you want, and try to implement it yourself. If you have problems repost what you tried.
-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to convert Python Multiple list to list

Postby priyanka_hdp » Wed May 15, 2013 11:07 am

thanks its done with below code
Code: Select all
seq=[[4, 1, False], [4, 2, False], [4, 3, False], [4, 4, False], [4, 5, False]]

[item[1] for item in seq]
[1, 2, 3, 4, 5]
priyanka_hdp
 
Posts: 5
Joined: Thu Mar 28, 2013 10:35 am

Re: How to convert Python Multiple list to list

Postby Mekire » Wed May 15, 2013 11:12 am

Indeed. Your problem is solved then?

-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 1 guest