Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Access front and rear element of Python tuple
Next article icon

The task of creating a tuple from astring and alistinPython involves combining elements from both data types into a single tuple. The list elements are added as individual items and the string is treated as a single element within the tuple.For example, given a = ["gfg", "is"] and b = "best", the goal is to create the tuple ('gfg', 'is', 'best').

Using + operator

+ operator is an efficient way to create a tuple from a list and a string . It works by concatenating two tuples, the list is first converted to a tuple and the string is wrapped in a single-element tuple (b,). It is preferred for its simplicity when combining fixed elements into a new tuple.

Python
a=["gfg","is"]# listb="best"# stringres=tuple(a)+(b,)print(res)

Output
('gfg', 'is', 'best')

Explanation: tuple(a) + (b,)converts the lista into a tuple and wraps the string bin a single-element tuple. Since tuples can only be concatenated with other tuples, the+ operatorcombines them into ('gfg', 'is', 'best').

Using * operator

* operator, known as iterable unpacking, is a modern way to create a tuple from a list and a string . It works by unpacking the elements of the list into a tuple alongside the string. This approach is particularly concise, often favored for combining multiple elements into a tuple without converting types explicitly.

Python
a=["gfg","is"]# listb="best"# stringres=(*a,b)print(res)

Output
('gfg', 'is', 'best')

Explanation: (*a, b) unpacks the list a into individual elements and places them in a new tuple, followed by the string b as the last element.

Using chain()

chain() from theitertools module is a powerful method for combining multiple iterables like lists and strings into a single sequence. It efficiently iterates over each element in sequence without creating intermediate lists, making it suitable for creating large tuples from diverse data sources.

Python
fromitertoolsimportchaina=["gfg","is"]# listb="best"# stringres=tuple(chain(a,[b]))print(res)

Output
('gfg', 'is', 'best')

Explanation: tuple(chain(a, [b]))useschain() from theitertools module to iterate over the lista and a single-elementlist [b]as if they were a single sequence.

Using append()

append() is a simple and direct approach when combining a string into an existing list before converting it to a tuple. This method modifies the original list by appending the string, after which the list is converted into a tuple. While easy to understand, it is less flexible as it alters the input list.

Python
a=["gfg","is"]# listb="best"# stringa.append(b)res=tuple(a)print(res)

Output
('gfg', 'is', 'best')

Explanation: a.append(b) adds the string b to the end of the lista andtuple(a) then converts this updated list into a tuple.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp