Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split tuple items to separate variables

Tags:

I have tuple in Python that looks like this:

tuple = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')

and I wanna split it out so I could get every item from tuple independent so I could do something like this:

domain = "sparkbrowser.com"
level = 0
url = "http://facebook.com/sparkbrowser"
text = "Facebook"

or something similar to that, My need is to have every item separated. I tried with .split(",") on tuple but I've gotten error which says that tuple doesn't have split option.

like image 229
dzordz Avatar asked Aug 22 '13 06:08

dzordz


People also ask

How do you unpack a tuple?

Python uses a special syntax to pass optional arguments (*args) for tuple unpacking. This means that there can be many number of arguments in place of (*args) in python. All values will be assigned to every variable on the left-hand side and all remaining values will be assigned to *args .

Can you slice a tuple?

Slicing. We can access a range of items in a tuple by using the slicing operator colon : . Slicing can be best visualized by considering the index to be between the elements as shown below. So if we want to access a range, we need the index that will slice the portion from the tuple.

How do you split a string in a tuple?

When it is required to convert a string into a tuple, the 'map' method, the 'tuple' method, the 'int' method, and the 'split' method can be used. The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.

Is unpacking possible in a tuple?

In python tuples can be unpacked using a function in function tuple is passed and in function, values are unpacked into a normal variable. The following code explains how to deal with an arbitrary number of arguments. “*_” is used to specify the arbitrary number of arguments in the tuple.


1 Answers

Python can unpack sequences naturally.

domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
like image 50
Ignacio Vazquez-Abrams Avatar answered Oct 24 '22 04:10

Ignacio Vazquez-Abrams