-1

I'm trying to convert each tuple inside users to a list. I tried the following code to achieve that:

usernames = ["Dude","Bro","Mister",]
passwords = ("bloke","black","nobbe",)
users = list(zip(usernames,passwords))


for i in users:
    i = list(i)

print(users)

My expected output for print(users) was: [['Dude', 'bloke'], ['Bro', 'black'], ['Mister', 'nobbe']]

But print(user) gave me the following, leaving the tuples unchanged: [('Dude', 'bloke'), ('Bro', 'black'), ('Mister', 'nobbe')]

How can convert each tuple inside users to a list?

  • So, nowhere do you modify your list in your code. This is an important point to understand - why did you *expect* the list to change here at all? – juanpa.arrivillaga Feb 23 '23 at 17:40

2 Answers2

0

You can do something as simple as

users = [list(u) for u in users]

or like this, if you don't want a list comprehension.

for index, u in enumerate(users):
    users[index] = list(u)

Please read this article to have a better understanding about lists in Python.

Pawel Kam
  • 1,684
  • 3
  • 14
  • 30
0

use for loop as below:

for i in range(len(users)):
    users[i] = list(users[i])
Ugur Yigit
  • 155
  • 7