Converting each element of a list to tuple

2024/11/17 1:36:14

to convert each element of list to tuple like following :

l = ['abc','xyz','test']

convert to tuple list:

newl = [('abc',),('xyz',),('test',)]

Actually I have dict with keys like this so for searching purpose I need to have these.

Answer

You can use a list comprehension:

>>> l = ['abc','xyz','test']
>>> [(x,) for x in l]
[('abc',), ('xyz',), ('test',)]
>>>

Or, if you are on Python 2.x, you could just use zip:

>>> # Python 2.x interpreter
>>> l = ['abc','xyz','test']
>>> zip(l)
[('abc',), ('xyz',), ('test',)]
>>>

However, the previous solution will not work in Python 3.x because zip now returns a zip object. Instead, you would need to explicitly make the results a list by placing them in list:

>>> # Python 3.x interpreter
>>> l = ['abc','xyz','test']
>>> zip(l)
<zip object at 0x020A3170>
>>> list(zip(l))
[('abc',), ('xyz',), ('test',)]
>>>

I personally prefer the list comprehension over this last solution though.

https://en.xdnf.cn/q/71272.html

Related Q&A

Python, Zeep response to pandas

I am tryng to conenct to a SOAP webservice and use pandas to put in on a table.Zeep give me this list:[{ssPeca: 103,ssQtd: 1,ssUn: un }, {ssPeca: 291A,ssQtd: 8,ssUn: un }, {ssPeca: 406B,ssQtd: 8,ssUn: …

Adjust the distance only between two subplots in matplotlib

I have 3 subplots (3 rows and 1 column). We can use fig.subplots_adjust(hspace=0.2) to adjust the distance between the subplots. this will change the distance between subplots for all case. How can I h…

Many-to-many multi-database join with Flask-SQLAlchemy

Im trying to make this many-to-many join work with Flask-SQLAlchemy and two MySQL databases, and its very close except its using the wrong database for the join table. Heres the basics... Ive got main_…

Merge two rows in the same Dataframe if their index is the same?

I have created a large Dataframe by pulling data from an Azure database. The construction of the dataframe wasnt simple as I had to do it in parts, using the concat function to add new columns to the d…

How do I use the Postgresql ANY operator in a NOT IN statement

Using Pyscopg2, how do I pass a Python list into an SQL statement using the ANY Operator?Normal Working SQL reads (See SQL Fiddle):SELECT * FROM student WHERE id NOT IN (3);Using Psycopg2 as below:Psy…

pass command-line arguments to runpy

I have two files, one which has a side effect I care about that occurs within the if __name__ == "__main__" guard:# a.py d = {} if __name__ == "__main__":d[arg] = helloThe second fi…

Altering my python path: helloworld.py returns command not found—

Massive apologies for this embarrassing question—Im using my MacBook Pro, running snow leopard, and using Python 2.7.1. Trying to run my first script and all the first pages of all my tutorials are la…

ImportError: cannot import name force_text

I have installed Python 2.7 and Django 1.4 in my CentOS machine and installed all dependencies for my existing project. When I run python manage.py runserver, I am getting the following traceback in my…

How can I select the pixels that fall within a contour in an image represented by a numpy array?

VI have a set of contour points drawn on an image which is stored as a 2D numpy array. The contours are represented by 2 numpy arrays of float values for x and y coordinates each. These coordinates are…

Get the value of specific JSON element in Python

Im new to Python and JSON, so Im sorry if I sound clueless. Im getting the following result from the Google Translate API and want to parse out the value of "translatedText":{"data"…