473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to convert a list of tuples to a list of lists?

2
[(1, red), (2, blue), (3, green)]

how do I convert to:

[[1, red], [2, blue], [3, green]]

thanks
Feb 26 '11 #1
4 7433
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> seq1 = [(1, 'red'), (2, 'blue'), (3, 'green')]
  2. >>>[list(item) for item in seq1]
  3. [[1, 'red'], [2, 'blue'], [3, 'green']]
  4. >>> 
Feb 26 '11 #2
JonPC
2
It's still returning a list of tuples:
Expand|Select|Wrap|Line Numbers
  1. seq1 = [(1, 'red'), (2, 'blue'), (3, 'green')][list(item) for item in seq1]
  2. print seq1
>>>
[(1, 'red'), (2, 'blue'), (3, 'green')]
>>>
Feb 27 '11 #3
bvdet
2,851 Expert Mod 2GB
You are not making an assignment of the new structure to seq1.
Feb 27 '11 #4
dwblas
626 Expert 512MB
You can also use a for() loop if you don't understand list comprehensions. Iterate through the list, cast each tuple to a list, and append this list to a new, outer list.
Feb 27 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
2
by: beliavsky | last post by:
Tuples, unlike lists, are immutable, which I crudely translate to mean "their contents cannot be changed". Out of habit, I use only lists, not tuples, in my code. But I wonder if this is poor style...
7
by: Bart Nessux | last post by:
I understand how a Python list can be broken into smaller lists by using slice like this: new_small_list_1 = BIG_LIST #0-1000 new_small_list_2 = BIG_LIST #1001-2000 new_small_list_3 =...
12
by: worzel | last post by:
I get what the difference is between a tuple and a list, but why would I ever care about the tuple's immuutability? Also, do you say 'too-ple' or 'chu-ple' - if you get my drift. (tomato or tomato...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
0
by: Dan Nordquist | last post by:
Hey everyone. I'm just getting my feet wet with ADSI (and VB.NET, and ASP.NET, really), but I'm wondering if there's a way to pull a list of distribution lists from an Exchange server using...
4
by: Sin Jeong-hun | last post by:
List<List<T>a=param; List<List<T>b=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
13
by: psbasha | last post by:
Hi, How to remove the duplicate data from List,Tuples and Dictionary?. Thanks in advance PSB
1
by: little83 | last post by:
Dear all I have created a list contains many lists of type int by using the following line List<List<int>> values = new List<List<int>>(); then I tried to fill one int value at each list
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.