a_student
a_student

Reputation: 43

Converting tuple from a list

I am having trouble converting a list into a tuple.

student = ['1712 Albert', '1703 Benny', '1799 Henry']

I want it to be

[(1712, 'Albert'), (1703, 'Benny'), (1799, 'Henry')]

So far I've done this

list1 = []
for elements in student:
    list1.append(tuple(elements.split(" ")))

However I'm getting the output:

[('1712', 'Albert'), ('1703', 'Benny'), ('1799', 'Henry')]

Which is not the same as the one above. How do I get rid of the quote marks for the numbers only.

Upvotes: 1

Views: 75

Answers (7)

Gabrio
Gabrio

Reputation: 388

take a look at this one-line solution

student = ['1712 Albert', '1703 Benny', '1799 Henry']
list1 = [ (int(year),name ) for year,name in [ x.split(" ") for x in student ] ]

Upvotes: 1

Sergii Shcherbak
Sergii Shcherbak

Reputation: 977

Try:

split_lists = [data.split(" ") for data in student]

students_list = [tuple([int(data[0]), data[1]]) for data in split_lists]

print(students_list)

You just need to convert a number string into an integer. If you are not always confident that the converted string is a number, you can populate the students_list using try-except when converting into int(), like so:

students_list = list()

for data in split_lists:
    sub_list = list()
    try:
        sub_list.append(int(data[0]))
    except ValueError:
        sub_list.append(data[0])
    sub_list.append(data[1])
    students_list.append(tuple(sub_list))

print(students_list)

Upvotes: 0

Alireza Afzal Aghaei
Alireza Afzal Aghaei

Reputation: 1235

another one line solution:

student = ['1712 Albert', '1703 Benny', '1799 Henry']    
list(map(lambda x:tuple(map(lambda y:int(y) if y.isnumeric() else y,x.split())),student))

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121594

You'll have to convert the first element to an integer explicitly:

list1 = []
for elements in student:
    id_, name = elements.split(None, 1)
    list1.append((int(id_), name))

I've updated the str.split() call a little: None tells the command to split on any whitespace, no matter how many characters (this includes tabs and newlines). The 1 tells str.split() to split just once, leaving whitespace in names in-tact. So "1703 Albert Ben" would become (1703, 'Albert Ben') in the output.

Demo:

>>> student = ['1712 Albert', '1703 Benny', '1799 Henry']
>>> list1 = []
>>> for elements in student:
...     id_, name = elements.split(None, 1)
...     list1.append((int(id_), name))
...
>>> list1
[(1712, 'Albert'), (1703, 'Benny'), (1799, 'Henry')]

Upvotes: 0

Utku Tombul
Utku Tombul

Reputation: 176

You can modify your code like this, to turn first element into a integer and then process them into a tuple.

for elements in student: 
    split_elements = elements.split(" ")
    split_elements[0] = int(split_elements[0])
    list1.append(tuple(split_elements))

Another approach would be running an additional loop to achieve so, but, both soes the same thing.

Upvotes: 0

Ubdus Samad
Ubdus Samad

Reputation: 1215

The quote marks are there because the numbers you have are in the format of a string. you need to change them into int().

l=[]
for element in list:
    i=element.split(' ')
    z=tuple(int(i[0]),i[1])
    l.append(z)

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249133

list1 = []
for elements in student:
    year, name = elements.split(" ", 1)
    list1.append((int(year), name)

Upvotes: 0

Related Questions